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