program zht;
var
n,i,p,bz,l,s:longint;
ans:int64;
a,bh:array[0..20000] of longint;
b:array[0..100000] of longint;
procedure dfs(x,cs:longint);
begin
if cs=5000 then begin bz:=1; p:=x; exit; end;
if bh[x]=1 then exit;
bh[x]:=1;
inc(l);
dfs(a[x],cs+1);
end;
function gcd(a:int64;b:longint):longint;
begin
if b<>0 then gcd:=gcd(b,a mod b) else gcd:=a;
end;
begin
assign(input,'officer.in');
assign(output,'officer.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=1 to n do
begin
bz:=0;
p:=0;
l:=0;
if bh[i]=1 then continue;
dfs(i,0);
while bz=1 do
begin
bz:=0;
dfs(p,0);
end;
inc(b[0]);
b[b[0]]:=l;
end;
ans:=1;
for i:=1 to b[0] do
begin
s:=gcd(ans,b[i]);
ans:=ans*(b[i] div s);
end;
writeln(ans);
close(input);
close(output);
end.