#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++) cin >> a[i];
  bool bhau = false;
  for (long long i = 1; i <= n; i++) {
    if (a[i] != i && a[a[i]] != i && a[a[i]] != a[i] && a[a[a[i]]] == i) {
      bhau = true;
      break;
    }
  }
  if (bhau) {
    cout << "YES" << endl;
  } else
    cout << "NO" << endl;
}
