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.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <hash_map>
#include <unordered_set>
#include <fstream>
#include <limits.h>

using namespace std;
typedef long long ll;


ll nod(ll a, ll b)
{
    return b ? nod(b, a%b) : a;
}

ll nok(ll a, ll b)
{
    return a / nod(a, b)*b;
}


int main(){
    
    //freopen("A-small-attempt2.in", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> v(n);
    for (ll i = 0; i < n; ++i)
    {
        cin >> v[i];
        v[i]--;
    }

    vector<vector<ll> > arr(n, vector<ll>(2 * n + 5));
    vector<ll> len(n, -1);
    vector<ll> pref(n, -1);
    ll mxpref = -1;
    for (ll i = 0; i < n; ++i)
    {
        arr[i][0] = i;
        map<ll , ll > ma;
        ma.insert(make_pair(i, 0));
        for (ll j = 1; j < 2*n+5; ++j)
        {
            arr[i][j] = v[arr[i][j - 1]];
            if (len[i] == -1)
            {
                auto it = ma.find(arr[i][j]);
                if (it != ma.end())
                {
                    len[i] = j - it->second;
                    pref[i] = j;
                    mxpref = max(pref[i], mxpref);
                }
                else
                {
                    ma.insert(make_pair(arr[i][j], j));
                }
            }
        }
    }
    ll k = LLONG_MAX;
    for (ll i = 1; i <= n; ++i)
    {

        bool fl = true;
        for (ll j = 0; j < n; ++j)
        {
            if (arr[j][i] != arr[j][2 * i])
            {
                fl = false;
                break;
            }
        }
        if (fl)
            k = min(k, i);
    }

    ll allnok = 1;

    for (int i = 0; i < n; ++i)
    {
        allnok = nok(allnok, len[i]);
    }

    k = min(allnok + mxpref, k);

    cout << k;
    


    return 0;
}

/*
5
5 4 3 2 1


*/