#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
#pragma GCC target "tune=native"
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  for (int& x : t) {
    cin >> x;
    --x;
  }
  for (int i = 0; i < (n); i++)
    if (t[t[t[i]]] == i) {
      cout << "YES" << endl;
      return 0;
    }
  cout << "NO" << endl;
}
