program move;
var
a,b:array[1..10001] of longint;
n,i,j,temp:integer;
he,pingjun,min,s:longint;
begin
assign(input,'move.in');
reset(input);
assign(output,'move.out');
rewrite(output);
readln(n);
for i:=1 to n do
begin
read(a[i]);
he:=he+a[i];
end;
pingjun:=he div n;
min:=maxlongint;
for i:=1 to n do
begin
temp:=a[1];
for j:=1 to n-1 do
a[j]:=a[j+1];
a[n]:=temp;
s:=0;
b:=a;
for j:=1 to n-1 do
begin
if b[j]>=pingjun then
begin
s:=s+(b[j]-pingjun);
b[j+1]:=b[j+1]+(b[j]-pingjun)
end
else
begin
s:=s+(pingjun-b[j]);
b[j+1]:=b[j+1]-(pingjun-b[j])
end
end;
if s<min
then min:=s
end;
writeln(min);
close(input);
close(output)
end.