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 JAVA):

Solution:
/*
 * For the brave souls who get this far: You are the chosen ones,
 * the valiant knights of programming who toil away, without rest,
 * fixing our most awful code. To you, true saviors, kings of men,
 * I say this: never gonna give you up, never gonna let you down,
 * never gonna run around and desert you. Never gonna make you cry,
 * never gonna say goodbye. Never gonna tell a lie and hurt you.
 * 
 * And Logic is the strongest weapon. 
 *
 */
//package Coding;

import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

public class p2 {

    private static InputStream stream;
    private static byte[] buf = new byte[1024];
    private static int curChar;
    private static int numChars;
    private static SpaceCharFilter filter;
    private static PrintWriter pw;
    //pw.print("Case #" + i + ": ");
    //private static int infi = (int) 1e9;
    //private static final int N = 1234567;
    private static final double MOD = 1e9 + 7;
    //Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

    public static int soln() {
        int n = nI();
        int[] a = nIArr(n);
        for (int i = 0; i < n; i++) {
            int cc = a[a[i] - 1];
            if (a[cc - 1] == i + 1) {
                pw.println("YES");
                return 0;
            }
        }
        pw.println("NO");
        return 0;
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader(System.in);
        //pw = new PrintWriter(new FileOutputStream("ans.txt"));
        //pw = new PrintWriter(new FileOutputStream("ans1.txt"));
        pw = new PrintWriter(System.out);
        soln();
        pw.close();
    }

    /*
     *********************************************************************************************
     */
    public static void InputReader(InputStream stream1) {
        stream = stream1;
    }

    private static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private static boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }

    private static int read() {
        if (numChars == -1) {
            throw new InputMismatchException();
        }
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0) {
                return -1;
            }
        }
        return buf[curChar++];
    }

    private static void pIArr(int[] man) {
        int l = man.length;
        for (int i = 0; i < l; i++) {
            pw.print(man[i] + " ");
        }
        pw.println();
    }

    private static void pCArr(char[] man) {
        int l = man.length;
        for (int i = 0; i < l; i++) {
            pw.print(man[i] + " ");
        }
        pw.println();
    }

    private static void pLArr(long[] man) {
        int l = man.length;
        for (int i = 0; i < l; i++) {
            pw.print(man[i] + " ");
        }
        pw.println();
    }

    private static void pDArr(double[] man) {
        int l = man.length;
        for (int i = 0; i < l; i++) {
            pw.print(man[i] + " ");
        }
        pw.println();
    }

    private static void pFArr(float[] man) {
        int l = man.length;
        for (int i = 0; i < l; i++) {
            pw.print(man[i] + " ");
        }
        pw.println();
    }

    private static int[] nIArr(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nI();
        }
        return a;
    }

    private static long[] nLArr(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nL();
        }
        return a;
    }

    private static double nD() {
        return Double.parseDouble(nS());
    }

    private static int nI() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    private static long nL() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    private static String nS() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    private static String nLi() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    private static boolean isSpaceChar(int c) {
        if (filter != null) {
            return filter.isSpaceChar(c);
        }
        return isWhitespace(c);
    }

    private interface SpaceCharFilter {

        public boolean isSpaceChar(int ch);
    }
}