#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) {
    if (i == arr[arr[i]]) continue;
    if (i == arr[arr[arr[i]]]) {
      cout << "YES\n";
      return 0;
    }
    if (i == n - 1) {
      cout << "NO\n";
      return 0;
    }
  }
  cout << "NO\n";
  return 0;
}
