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