#include <bits/stdc++.h>
using namespace std;
int num[5009];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
  }
  for (int i = 1; i <= n; i++) {
    int first = i;
    int sec = num[i];
    int third = num[sec];
    int tri = num[third];
    if (tri == first) {
      cout << "YES\n";
      return 0;
    }
  }
  cout << "NO\n";
  return 0;
}
