program officer;
var
data:array [1..10001] of integer;
step:array [1..10001] of qword;
n,i,now:longint;
a,b,tmp,t:qword;
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
a:=step[i];b:=step[i-1];
if a<b then begin
tmp:=a;
a:=b;
b:=tmp;
end;
while b>0 do begin
tmp:=a mod b;
a:=b;
b:=tmp;
end;
t:=a;
if step[i]<>0 then step[i]:=step[i] div t*step[i-1] else step[i]:=t;
end;
writeln (step[n]);
close (output);
end.