Problem: As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.

Examples

Input

5
2 4 5 1 3


Output

YES


Input

5
5 5 5 5 1


Output

NO

Note

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
bool f[10000007];
vector<int> prime;
void sieve() {
  f[0] = f[1] = 1;
  for (int i = 2; i < 10000007; ++i) {
    if (f[i]) continue;
    prime.push_back(i);
    for (int j = i * i; i <= 10000 && j < 10000007; j += i) f[j] = 1;
  }
}
vector<int> factor;
void getFactor(int n) {
  for (int i = 2; i <= n / i; ++i) {
    while (n % i == 0) {
      factor.push_back(i);
      n /= i;
    }
  }
  if (n != 1) factor.push_back(n);
}
long long MaxSubarray(long long arr[], int n) {
  long long best = -1e18, sum = 0;
  for (int i = 0; i < n; ++i) {
    sum = max(arr[i], arr[i] + sum);
    best = max(best, sum);
  }
  return best;
}
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
long long fast_power(long long b, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * b) % 1000000007;
    b = (b * b) % 1000000007;
    p /= 2;
  }
  return res;
}
int factorialTrailingZeros(int n) {
  int cnt = 0;
  for (int i = 5; n >= i; i *= 5) cnt += n / i;
  return cnt;
}
long long fac(int st, int en) {
  if (st > en) return -1;
  long long ret = 1;
  if (st == 0) ++st;
  for (int i = st; i <= en; ++i) ret *= i;
  return ret;
}
long long P(int n, int k) {
  if (n < k) return -1;
  if (n == 0) return 0;
  return fac(n - k + 1, n);
}
long long C(int n, int k) {
  if (n < k) return -1;
  int k1 = n - k;
  return fac(max(k1 + 1, k + 1), n) / fac(0, min(k1, k));
}
long long C_over(int n, int k) {
  if (n < k) return -1;
  long long ret = 1;
  int k1 = n - k;
  vector<int> a, b;
  for (int i = max(k1 + 1, k + 1); i <= n; ++i) a.push_back(i);
  for (int i = 2; i <= min(k1, k); ++i) b.push_back(i);
  for (int i = 0; i < a.size(); ++i) {
    for (int j = 0; j < b.size(); ++j) {
      int x = GCD(a[i], b[j]);
      a[i] /= x, b[j] /= x;
    }
  }
  for (int i = 0; i < a.size(); ++i) ret *= a[i];
  return ret;
}
template <typename T>
void printPQ(T& q) {
  while (!q.empty()) {
    std::cout << q.top() << " ";
    q.pop();
  }
  std::cout << '\n';
}
int n, arr[5005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", arr + i);
  for (int i = 1; i <= n; ++i) {
    if (arr[arr[arr[i]]] == i) exit(puts("YES"));
  }
  puts("NO");
  return 0;
}