#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, y, z, f = 0;
  pair<long long, long long> p[5001];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].first;
    p[i].second = (i + 1);
  }
  for (int i = 1; i <= n; i++) {
    x = p[i].first;
    y = p[x].first;
    z = p[y].first;
    if (z == i) {
      f = 1;
      break;
    }
  }
  cout << (f ? "YES" : "NO");
  return 0;
}
