Program move;
Type
sc=array [1..10000] of int64;
Var
i,j,k:longint;
min:int64;
n:longint;
s,s1:sc;
p,temp,d:int64;
Begin
assign(input,'move.in');
assign(output,'move.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do read(s[i]);
min:=99999999;
p:=0;
for i:=1 to n do p:=p+s[i];
p:=p div n;
for i:=1 to n do begin
k:=0;
for j:=i to n do begin
k:=k+1;
s1[k]:=s[j];
end;
for j:=1 to (i-1) do begin
k:=k+1;
s1[k]:=s[j];
end;
d:=0;
k:=0;
while (d<min)and(k<(n-1)) do begin
k:=k+1;
temp:=s1[k]-p;
s1[k]:=p;
s1[k+1]:=s1[k+1]+temp;
d:=d+abs(temp);
end;
if (d<min)and(k=n-1) then min:=d;
end;
writeln(min);
close(input);
close(output);
End.