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