program ex;
var
a,b:array[1..10000] of longint;
f:array[1..10000] of boolean;
x,z,i,j,n,m:longint;
procedure init;
begin
assign(input,'officer.in');
assign(output,'officer.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do readln(a[i]);
close(input);
for i:=1 to 10000 do f[i]:=true;
end;
function qiu(a,b:longint):longint;
var
c,d,t:longint;
begin
c:=a*b;
t:=a mod b;
while t<>0 do
begin
a:=0;
a:=b;
b:=t;
t:=a mod b;
end;
qiu:=c div b
end;
begin
init;
x:=0;
for i:=1 to n do
begin
if f[i]=true then
begin
j:=i;z:=1;f[j]:=false;
while a[j]<>i do
begin
j:=a[j];
f[j]:=false;
inc(z);
end;
inc(x);
b[x]:=z;
end;
end;
m:=b[1];
for i:=2 to x do
begin
m:=qiu(m,b[i]);
end;
writeln(m);
close(output);
end.