#include <bits/stdc++.h>
using namespace std;
const int maxn = 100050;
int f[5005] = {0};
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> f[i];
  }
  int ok = 0;
  for (int i = 1; i <= n; ++i) {
    int first = f[i];
    if (first <= n && first > 0) {
      int second = f[first];
      if (second <= n && second > 0) {
        if (f[second] == i) {
          ok = 1;
          break;
        }
      }
    }
  }
  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
