program tt(input,output);
var
back,data,seat:array[1..10000] of integer;
n,i,j,head,step,temp:integer; num:extended;
procedure change(a,b:integer);
begin
if (b=head) and (a=n)
then
begin
back[seat[a]]:=b;
back[seat[a-1]]:=-1;
head:=seat[a];
exit;
end;
back[seat[a-1]]:=b;
back[seat[a]]:=back[b];
back[b]:=seat[a];
end;
function check:boolean;
var
i,j:integer;
begin
i:=head; j:=1;
check:=true;
repeat
if i <> j then check:=false;
seat[j]:=i;
i:=back[i];
inc(j);
until i=-1;
end;
begin
assign(input,'officer.in');
reset(input);
assign(output,'officer.out');
rewrite(output);
readln(n);
for i:= 1 to n do
readln(data[i]);
close(input);
for i:= 1 to n do
begin
seat[i]:=i;
back[i]:=i+1;
end;
back[n]:=-1;
head:=1; num:=0;
repeat
num:=num+1;
step:= trunc(num) mod n;
if step=0 then step:=n;
if seat[step]= head then head:= back[head];
change(step,seat[data[step]]);
until check;
writeln(num:0:0);
close(output);
end.