program officer;
var
data:array [1..10000] of integer;
step:array [1..10000] of int64;
n,i,now:longint;
t1,t2:int64;
function gcd(a,b:longint):longint;
var tmp:longint;
begin
if a<b then begin
tmp:=a;
a:=b;
b:=tmp;
end;
if b=0 then exit(a);
while b>0 do begin
tmp:=a mod b;
a:=b;
b:=tmp;
end;
exit(a);
end;
begin
assign (input,'officer.in');
reset (input);
readln (n);
for i:=1 to n do readln (data[i]);
close (input);
assign (output,'officer.out');
rewrite (output);
fillchar (step,sizeof(step),0);
for i:=1 to n do begin
now:=data[i];step[i]:=1;
if now=i then step[i]:=0 else begin
while now<>i do begin
now:=data[now];
step[i]:=step[i]+1;
end;
end;
end;
for i:=2 to n do begin
t1:=step[i]*step[i-1];
t2:=gcd(step[i],step[i-1]);
step[i]:=t1 div t2;
end;
writeln (step[n]);
close (output);
end.