#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i;
  cin >> n;
  int a[n + 1];
  a[0] = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (n == 2) {
    cout << "NO";
    return 0;
  }
  for (i = 1; i <= n; i++) {
    int x;
    x = a[i];
    x = a[x];
    x = a[x];
    if (x == i) {
      cout << "YES";
      return 0;
    }
  }
  cout << "NO";
  return 0;
}
