#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int i, j, k = 0, n, x, c = 0, m = 1, d = 1000000007, a = 1,
                      b = 1000000007;
  cin >> n;
  vector<int> graph(n + 1);
  for (i = 1; i < n + 1; i++) {
    cin >> graph[i];
  }
  for (i = 1; i < n + 1; i++) {
    if (graph[graph[graph[i]]] == i) {
      cout << "YES";
      return;
    }
  }
  cout << "NO";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  solve();
}
