program officer;
type arr=array[1..10000] of longint;
var i,n:longint; m:int64;
A,B:arr;
function Orded(A:arr; n:longint):boolean;
var i:longint;
begin
for i:=1 to n do if A[i]<>i then exit(false);
exit(true);
end;
procedure Move(A:arr; var B:arr; n:longint);
var i:integer; C:arr;
begin
for i:=1 to n do C[A[i]]:=B[i];
B:=C;
end;
begin
assign(input,'officer.in'); reset(input);
assign(output,'officer.out'); rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(A[i]);
B[i]:=i;
end;
m:=0;
repeat
m+=1;
Move(A,B,n);
until Orded(B,n);
writeln(m);
close(input); close(output);
end.