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 f_{i}, where 1 ≤ f_{i} ≤ n and f_{i} ≠ 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 f_1, f_2, ..., f_{n} (1 ≤ f_{i} ≤ n, f_{i} ≠ i), meaning that the i-th plane likes the f_{i}-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.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
a = list([int(x) - 1 for x in input().split()])

ans = False

for i in range(n):
    if a[a[a[i]]] == i:
        ans = True
        break

print('YES' if ans else 'NO')


n=int(input())
A=[int(i) for i in input().split(" ")]
for i in range(n):
    A[i]-=1
c=0
for i in range(n):
    if i==A[A[A[i]]]:
        c=1
if c:
    print('YES')
else:
    print('NO')


def read_nums():
    return [int(x) for x in input().split()]


def is_triangle(nums):
    for index, num in enumerate(nums):
        if nums[nums[num-1]-1] - 1 == index:
            return 'YES'
    return 'NO'


def main():
    input()
    nums = read_nums()
    print(is_triangle(nums))


def __starting_point():
    main()

__starting_point()

n = int(input())
a = list(map(int, input().split()))
a = list(map(lambda x: x - 1, a))
f = False
for i in a:
    if a[a[a[i]] ] == i:
        f = True
        break
print("YES" if f else "NO")

n = int(input())
a = list(map(int,input().split()))
if n==2:
    print ('NO')
else:
    f=0
    for i in range(n):
        a[i]-=1
    for i in range(n):
        if a[a[a[i]]] == i:
            f=1
            break
    if f:
        print ('YES')
    else:
        print ('NO')



n = int(input())
a = [int(z) for z in input().split()]
a = [0] + a
for i in range(1, n):
    if (a[a[a[i]]] == i):
        print("YES")
        return
print("NO")

def run():
    input()
    s = [int(x) for x in input().split()]

    for i in range(len(s)):
        s1 = s[i]
        s2 = s[s1-1]
        s3 = s[s2-1]
        if s3-1==i:
            print('YES')
            return
    print('NO')
run()

n = int(input())
x = list(map(int,input().split()))
for i in range(n):
	if i == x[x[x[i]-1]-1]-1:
		print('YES')
		break
else:
	print('NO')

import sys, math

#f = open('input_0', 'r')
f = sys.stdin

n = int(f.readline())
l = list(map(int, f.readline().split()))
l = [x-1 for x in l]

found = False
for i in range(n):
  if l[i] != i and l[l[i]] != i and l[l[i]] != l[i] and l[l[l[i]]] == i:
    found = True
    print('YES')
    break
if not found:
  print('NO')


n = int(input())
a = [int(i) - 1 for i in input().split()]
for i in range(n):
    if a[a[a[i]]] == i:
        print("YES")
        return
print("NO")

n = int(input())
l = list(map(int,input().split()))
for i in range(n):
	if l[i] == l[l[l[l[i] - 1] - 1] - 1]:
		print('YES')
		n = -1
		break
if n != -1:
	print('NO')

n = int(input())
a = list(map(int, input().split()))

for i in range(len(a)):
    if a[a[i] - 1] - 1 != i and a[a[a[i] - 1] - 1] - 1 == i:
        print("YES")
        return
print("NO")


import sys

def read_int():
    return int(input())

def read_ints():
    return [int(x) for x in input().split()]

n = read_int()
a = read_ints()

for i in range(n):
    if a[a[a[i] - 1] - 1] == i + 1:
        print('YES')
        return

print('NO')


n = int(input())
s = [int(i)-1 for i in input().split()]
c = [s[s[s[i]]] for i in range(n)]
for i in range(n):
    if c[i] == i:
        print("YES")
        break
else:
    print("NO")

n = int(input())
a = [int(x) for x in input().split()]
flag = False
for i in range(n):
    if (a[a[a[i] - 1] - 1] - 1) == i:
        flag = True
        break
print('YES' if flag else 'NO')


n = int(input())

f = list([int(x) - 1 for x in input().split()])

for i in range(len(f)):
    if f[f[f[i]]] == i and f[f[i]] != i:
        print('YES')
        return

print('NO')


#This code sucks, you know it and I know it.  
#Move on and call me an idiot later.

n = int(input())

l = list(map(int, input().split()))

for i in l:
    a = i
    b = l[a-1]
    c = l[b-1]
    if a == l[c-1] and a!=b and b!=c and c!=a:
        print("YES")
        return
print("NO")

R = lambda : list(map(int, input().split()))

n = int(input())
f = list(R())
d = {}

for i in range(n):
    d[i+1] = f[i]

def solve(d,n):
    for i in range(n):
        b = d[i+1]
        c = d[b]
        a = d[c]
        if i + 1 == a:
            return "YES"
    
    return "NO"

print(solve(d,n))
        



#!/usr/bin/env python3
from sys import stdin, stdout

def rint():
    return map(int, stdin.readline().split())
#lines = stdin.readlines()

n = int(input())
f = list(rint())
f = [-1] + f
for i in range(1,n+1):
    a = i
    b = f[a]
    c = f[b]
    d = f[c]
    if a == d:
        print("YES")
        return

print("NO")

def func(v):
    n = len(v)
    for i in range(n):
        if v[v[v[i]]] == i:
            return True
    return False
def __starting_point():
    n = int(input())
    v = list(map(int, input().split()))
    for i in range(len(v)):
        v[i] -= 1
    if func(v):
        print("YES")
    else:
        print("NO")
__starting_point()

n=int(input())
l = list(map(int,input().split()))
for i in range(n):
    if l[l[l[i-1]-1]-1]==i:
        print('YES')
        quit()
print('NO')


n = int(input())
l = list(map(int, input().split()))

flag = False
for i in range(n):
    if l[l[l[i] - 1] - 1] == i + 1:
        print('YES')
        flag = True
        break

if not flag:
    print('NO')


n = int(input())
L = list(map(int, input().split()))
f = False
for i in range(n):
    j = L[i]
    k = L[j - 1]
    m = L[k - 1]
    if i == m - 1:
        f = True
        break
print('YES' if f else 'NO')

n = int(input())

lst = []
for x in input().split():
    lst.append(int(x) - 1)

flag = False
for x in range(len(lst)):
    if x == lst[lst[lst[x]]]:
        flag = True
        break

if flag:
    print("YES")
else:
    print("NO")


s = int(input())
a = [int(i) for i in input().split()]
for idx, num in enumerate(a):
    if a[a[num-1]-1]==idx+1:
        print("YES")
        return
print("NO")