program move;
var
a,b:array[1..20001] of longint;
n,i,j: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];
a[n+i]:=a[i]
end;
pingjun:=he div n;
min:=maxlongint;
for i:=1 to n do
begin
s:=0;
b:=a;
for j:=i to i+n-2 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.