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:
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;

public class Template {

    class Edge{
        int to;
        int num;
        Edge(int to, int num){
            this.to = to;
            this.num = num;
        }
    }

    class Vertex{
        int x, y;
        Vertex(int x, int y){
            this.x = x;
            this.y = y;
        }

    }
    String fileName = "";
    int MODULO = 1000*1000*1000+7;
    ArrayList<Integer>[] graph;
    boolean[] used;
    int[] length;
    boolean ans = false;
    void solve() throws IOException {
        int n = readInt();
        int[] a = readIntArray(n);
        for(int i=0; i<n; ++i){
            int dist = 0;
            int cur = i;
            HashSet<Integer> st = new HashSet<>();
            while(dist<3){
                st.add(cur);
                cur = a[cur] - 1;
                dist++;
            }
            //out.println(dist);
            if(cur == i && st.size() == 3){
              out.println("YES");
              return;
            }
        }
        out.println("NO");

    }
    void dfs(int from, int dist){
        used[from] = true;
        length[from] = dist;
        for(int to: graph[from]){
            if(!used[to]){
                dfs(to, dist+1);
            }else{
                if(length[from] - length[to] ==3){
                    ans = true;
                }
            }
        }
    }

    boolean isLower(char c){
        return c >= 'a' && c <= 'z';
    }

    ////////////////////////////////////////////////////////////
    class Pair{
        int num;
        int val;
        Pair(int num, int val ){
            this.num = num;
            this.val = val;
        }
    }
    /*
    class SegmentTree {
        int start = 0;
        int end = 0;
        long delta;
        int n;
        long result;
        long[] t;
        boolean[] lazy;
        int[] length;
        Pair[] segments;
        SegmentTree(int n, int[] a){
            this.n = n;
            t = new long[4*n];
            lazy = new boolean[4*n];
            length = new int[4*n];
            segments = new Pair[4*n];
            build(a, 1, 1, n);
        }

        void build(int[] a, int v, int tl, int tr){
            if(tl>tr) return;
            if(tl == tr){
                t[v] = a[tl];
                length[v] = 1;
                return;
            }
            int mid = (tl+tr)/2;
            build(a, 2*v, tl, mid);
            build(a, 2*v+1, mid+1, tr);
            length[v] = tr - tl +1;
            t[v] = t[2*v] + t[2*v+1];
        }
        void pushVertex(int v, int vLeft, int vRight){
            lazy[vLeft] += lazy[v];
            lazy[vRight] += lazy[v];
            lazy[v] = 0;
        }

        void updateVertex(int v, int vLeft, int vRight){
            t[v] = getSum(vLeft) + getSum(vRight);
        }

        long getSum(int v){
            return t[v] + lazy[v] * length[v];
        }

        void incSegment(int start, int end, long delta){
            this.start = start;
            this.end = end;
            this.delta = delta;
            incSegment(1, 1, n);
        }

        void incSegment(int v, int l, int r){
            if(start <= l && r <= end){
                lazy[v] += delta;
            }else{
                int mid = (r+l)/2;
                int vLeft = (v << 1);
                int vRight = vLeft + 1;
                pushVertex(v, vLeft, vRight);

                if(start <= mid) incSegment(vLeft, l, mid);
                if(end >= mid +1) incSegment(vRight, mid+1, r);

                updateVertex(v, vLeft, vRight);
            }
        }

        long getSegment(int start, int end){
            this.start = start;
            this.end = end;
            this.result = 0;
            getSegment(1, 1, n);
            return result;
        }
        void getSegment(int v, int l, int r){
            if(start <= l && r <= end){
                result += getSum(v);
            }else{
                int mid = (r+l)/2;
                int vLeft = (v << 1);
                int vRight = vLeft + 1;
                pushVertex(v, vLeft, vRight);

                if(start <= mid) getSegment(vLeft, l, mid);
                if(end >= mid +1) getSegment(vRight, mid+1, r);

                updateVertex(v, vLeft, vRight);
            }
        }
    }
*/
    class Fenwik {
        long[] t;
        int length;

        Fenwik(int[] a) {
            length = a.length + 100;
            t = new long[length];

            for (int i = 0; i < a.length; ++i) {
                inc(i, a[i]);
            }
        }

        void inc(int ind, int delta) {
            for (; ind < length; ind = ind | (ind + 1)) {
                t[ind] += delta;
            }
        }

        long getSum(int r) {
            long sum = 0;
            for (; r >= 0; r = (r & (r + 1)) - 1) {
                sum += t[r];
            }
            return sum;
        }
    }
    int gcd(int a, int b){
        return b == 0 ? a : gcd(b, a%b);
    }

    long binPow(long a, long b, long m) {
        if (b == 0) {
            return 1;
        }
        if (b % 2 == 1) {
            return ((a % m) * (binPow(a, b - 1, m) % m)) % m;
        } else {
            long c = binPow(a, b / 2, m);
            return (c * c) % m;
        }

    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        new Template().run();
    }

    void run() throws NumberFormatException, IOException {
        solve();
        out.close();
    };

    BufferedReader in;
    PrintWriter out;

    StringTokenizer tok;
    String delim = " ";
    Random rnd = new Random();

    Template() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            if (fileName.isEmpty()) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader(fileName + ".in"));
                out = new PrintWriter(fileName + ".out");
            }

        }
        tok = new StringTokenizer("");
    }

    String readLine() throws IOException {
        return in.readLine();
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            String nextLine = readLine();
            if (null == nextLine) {
                return null;
            }

            tok = new StringTokenizer(nextLine);
        }
        return tok.nextToken();
    }

    int readInt() throws NumberFormatException, IOException {
        return Integer.parseInt(readString());
    }
    byte readByte() throws NumberFormatException, IOException {
        return Byte.parseByte(readString());
    }
    int[] readIntArray (int n) throws NumberFormatException, IOException {
        int[] a = new int[n];
        for(int i=0; i<n; ++i){
            a[i] = readInt();
        }
        return a;
    }
    long readLong() throws NumberFormatException, IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws NumberFormatException, IOException {
        return Double.parseDouble(readString());
    }
}