program checkg;

uses
    testlib, sysutils;

const
    maxn = 100;

function gcd(x, y: longint): longint;
var
    t: longint;
begin
    while y <> 0 do
    begin
        t := y;
        y := x mod y;
        x := t;
    end;

    gcd := x;
end;

var
    i, j, k, n: longint;
    u: array [1..maxn] of boolean;
    a: array [1..maxn] of longint;
    cl: array [1..maxn] of longint;
    ja, pa: longint;

function calc: longint;
var
    i, r: longint;
begin
    fillchar(u, sizeof(u), 0);
    fillchar(cl, sizeof(cl), 0);

    for i := 1 to n do
    begin
        if not u[i] then
        begin
            j := i;
            k := 0;
            repeat
                inc(k);
                u[j] := true;
                j := a[j];
            until j = i;
            cl[i] := k;
        end;
    end;

    r := 1;
    for i := 1 to n do
    begin
        if cl[i] > 0 then
        begin
            j := gcd(r, cl[i]);
            r := r * (cl[i] div j);
        end;
    end;

    calc := r;
end;

begin
    n := inf.readlongint;

    for i := 1 to n do
        a[i] := ans.readlongint;

    ja := calc;

    fillchar(u, sizeof(u), 0);
    for i := 1 to n do
    begin
        a[i] := ouf.readlongint;
        if (a[i] < 1) or (a[i] > n) then
            quit(_wa, 'invalid number: ' + inttostr(a[i]));
        if u[a[i]] then
            quit(_wa, 'not permutation, duplicate number: ' + inttostr(a[i]));
        u[a[i]] := true;
    end;

    pa := calc;

    if pa < ja then
        quit(_wa, 'not optimal, expected: ' + inttostr(ja) + ', found: ' + inttostr(pa));
    if pa > ja then
        quit(_fail, 'better then jury''s, expected: ' + inttostr(ja) + ', found: ' + inttostr(pa));

    quit(_ok, inttostr(ja));
end.