#include <bits/stdc++.h>
const long long MOD = 1e9;
const int EPS = 1e-6;
const int MAXX = 5e3 + 3;
const double PI = acos(-1);
using namespace std;
int arr[MAXX];
int main() {
  ios_base::sync_with_stdio(0);
  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) return cout << "YES" << endl, 0;
  }
  cout << "NO" << endl;
  return 0;
}
