#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b[5010], n, m;
  n = 0;
  cin >> a;
  for (int i = 1; i <= a; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= a; i++) {
    if (b[i] != i) {
      m = b[i];
      if (b[m] != m && b[m] != i) {
        int m1 = b[m];
        if (b[m1] != m1 && b[m1] == i) {
          n = 1;
        }
      }
    }
  }
  if (n == 0) {
    cout << "NO" << endl;
  }
  if (n == 1) {
    cout << "YES" << endl;
  }
}
