program officer;
var
i:longint;
n,t,max,min:int64;
sz,ans:array[1..10000]of int64;
function gcd(a,b:int64):int64;
begin
if a mod b=0 then gcd:=b else gcd:=gcd(b,a mod b);
end;
begin
assign(input,'officer.in');
assign(output,'officer.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
readln(sz[i]);
for i:=1 to n do
begin
ans[i]:=1;
t:=sz[i];
while t<>i do
begin
t:=sz[t];
ans[i]:=ans[i]+1;
end;
end;
max:=ans[1];
for i:=2 to n do
begin
if ans[i]>=max then max:=gcd(ans[i],max) else max:=gcd(max,ans[i]);
end;
min:=max;
for i:=1 to n do
min:=min*(ans[i] div max);
writeln(min);
close(input);
close(output);
end.