#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, current_value, next_value, third_value;
  cin >> n;
  long long int arr[5002];
  for (long long int i = 1; i <= n; i++) cin >> arr[i];
  long long int flag = 0;
  for (long long int i = 1; i <= n; i++) {
    current_value = arr[i];
    next_value = arr[current_value];
    third_value = arr[next_value];
    if (i == third_value) {
      flag = 1;
      break;
    }
  }
  if (flag == 1)
    cout << "YES";
  else
    cout << "NO";
}
