program xmz;
var
n,a,max,s,s1,min:longint;
x:array[0..10000]of longint;
f,f1:text;
procedure suan(b:longint);
var
i,j:longint;
y:array[0..10000]of longint;
begin
s1:=0;
for i:=0 to n do
y[i]:=x[i];
for j:=b to b+n do
begin
inc(s1,abs(s-y[j mod (n+1)]));
inc(y[(j+1)mod (n+1)],y[j mod (n+1)]-s);
y[j mod (n+1)]:=s;
end;
end;
begin
assign(f,'move.in');assign(f1,'move.out');
reset(f);rewrite(f1);
read(f,n);dec(n);
for a:=0 to n do
begin read(f,x[a]);s:=s+x[a];end;
s:=s div (n+1);
min:=99999999;
for a:=0 to n do
begin
suan(a);
if min>s1 then min:=s1;
end;
write(f1,min);
close(f1);close(f);
end.