#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

#define mpr make_pair
#define pr pair<int,long long>
#define st string
#define vint vector<int>
#define vpr vector<pr>
#define fr first
#define sc second
#define add push_back
#define del pop_back
#define qdel pop
inline int scn() { int hg; cin>>hg; return hg; };

const int MOD = 1000 * 1000 * 1000 + 7;
const int N=200006;
const long long INF=1000000000000000007;

int a[5006];

int main(){
    //cout<<INF<<endl;
    ios_base::sync_with_stdio(false);
    int n=scn();
    if (n==2){
        cout<<"NO"<<endl;
    }
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    for (int i=1;i<=n;i++){
        if (a[a[a[i]]]==i){
            cout<<"YES"<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
	return 0;
}