Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1, 2, ..., *n*} is such function <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be90664fb67fc59aea0742746b0626ddab543582.png" style="max-width: 100.0%;max-height: 100.0%;"/>, that for any <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/75df05ed78f11fc97381dcb0cd5b104b9483ca8f.png" style="max-width: 100.0%;max-height: 100.0%;"/> the formula *g*(*g*(*x*)) = *g*(*x*) holds.

Let's denote as *f*<sup class="upper-index">(*k*)</sup>(*x*) the function *f* applied *k* times to the value *x*. More formally, *f*<sup class="upper-index">(1)</sup>(*x*) = *f*(*x*), *f*<sup class="upper-index">(*k*)</sup>(*x*) = *f*(*f*<sup class="upper-index">(*k* - 1)</sup>(*x*)) for each *k* &gt; 1.

You are given some function <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/58da889a2e5c65797055ac8eba57b4a01abee69c.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Your task is to find minimum positive integer *k* such that function *f*<sup class="upper-index">(*k*)</sup>(*x*) is idempotent.
In the first line of the input there is a single integer *n* (1 ≤ *n* ≤ 200) — the size of function *f* domain.

In the second line follow *f*(1), *f*(2), ..., *f*(*n*) (1 ≤ *f*(*i*) ≤ *n* for each 1 ≤ *i* ≤ *n*), the values of a function.
from math import *

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def main():
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        a[i] = a[i] - 1
    dp = [0] * n
    for i in range(n):
        if i == a[i]:
            dp[i] = 1
    for i in range(n + 1):
        for j in range(n):
            if dp[a[j]] != 0 and dp[j] == 0:
                dp[j] = dp[a[j]] + 1
    bad = [0] * n
    for i in range(n):
        bad[i] = dp[i]
    MAX = max(dp)
    dp = [0] * n
    num = 1
    for i in range(n):
        if bad[i] != 0:
            continue
        if dp[i] == 0:
            cur = a[i]
            dp[i] = num
            while cur != i:
                dp[cur] = num
                cur = a[cur]
            num += 1
    ans = 1
    gg = 0
    for i in range(1, num):
        tmp = 0
        for j in range(n):
            if dp[j] == i:
                tmp += 1
        ans *= tmp
        if gg == 0:
            gg = tmp
        else:
            gg = gcd(gg, tmp)
    if num == 2:
        gg = 1
    if gg == 0:
        print(max((MAX - 1), 1))
        exit(0)
    ans = ans // gg
    k = 1
    while (ans * k < max(MAX - 1, 1)):
        k += 1
    ans *= k
    print (ans)
main()