
import java.util.Scanner;

class Main {

    String Name;
    int V;
    boolean T;

    Main(String Name, int V, boolean T) {
        this.Name = Name;
        this.V = V;
        this.T = T;
    }

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        int N = Input.nextInt();
        Main A[] = new Main[N];
        for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        }
        int C = 1;
        boolean TF = false;
        int X = A[0].V;
        A[0].T = true;
        while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        }

    }

}
//<<< Copying >>> Alert
//If(PersonCopy){System.out.print("You Are Not Learning"); import java.util.Scanner; Scanner java.util class Main {

    String Name;
    int V;
    boolean T;

    Main(String Name, int V, boolean T) {
        this.Name = Name;
        this.V = V;
        this.T = T;
    }

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        int N = Input.nextInt();
        Main A[] = new Main[N];
        for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        }
        int C = 1;
        boolean TF = false;
        int X = A[0].V;
        A[0].T = true;
        while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        }

    }

} Main String Name; Name int V; V boolean T; T Main(String Name, int V, boolean T) {
        this.Name = Name;
        this.V = V;
        this.T = T;
    } Main {
        this.Name = Name;
        this.V = V;
        this.T = T;
    } this.Name = Name this.Name this this.Name Name this.V = V this.V this this.V V this.T = T this.T this this.T T String Name Name int V V boolean T T public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        int N = Input.nextInt();
        Main A[] = new Main[N];
        for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        }
        int C = 1;
        boolean TF = false;
        int X = A[0].V;
        A[0].T = true;
        while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        }

    } main {
        Scanner Input = new Scanner(System.in);
        int N = Input.nextInt();
        Main A[] = new Main[N];
        for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        }
        int C = 1;
        boolean TF = false;
        int X = A[0].V;
        A[0].T = true;
        while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        }

    } Scanner Input = new Scanner(System.in); Input new Scanner(System.in) int N = Input.nextInt(); N Input.nextInt() Input.nextInt Input Main A[] = new Main[N]; A new Main[N] N for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        } int I = 0; int I = 0; I 0 I < N I N I++ I++ I {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        } {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        } A[I] = (new Main("B" + I, Input.nextInt() - 1, false)) A[I] A I (new Main("B" + I, Input.nextInt() - 1, false)) int C = 1; C 1 boolean TF = false; TF false int X = A[0].V; X A[0].V A[0] A 0 A[0].V A[0].T = true A[0].T A[0] A 0 A[0].T true while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        } !TF TF {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        } if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            } A[X].Name.equals("B1") A[X].Name.equals A[X].Name A[X] A X A[X].Name "B1" {
                System.out.println(C);
                TF = true;
            } System.out.println(C) System.out.println System.out System System.out C TF = true TF true if (A[X].T) {
                System.out.println("-1");
                TF = true;
            } A[X].T A[X] A X A[X].T {
                System.out.println("-1");
                TF = true;
            } System.out.println("-1") System.out.println System.out System System.out "-1" TF = true TF true A[X].T = true A[X].T A[X] A X A[X].T true X = A[X].V X A[X].V A[X] A X A[X].V C++ C String[] args args

import java.util.Scanner;

class Main {

    String Name;
    int V;
    boolean T;

    Main(String Name, int V, boolean T) {
        this.Name = Name;
        this.V = V;
        this.T = T;
    }

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);
        int N = Input.nextInt();
        Main A[] = new Main[N];
        for (int I = 0; I < N; I++) {
            A[I] = (new Main("B" + I, Input.nextInt() - 1, false));
        }
        int C = 1;
        boolean TF = false;
        int X = A[0].V;
        A[0].T = true;
        while (!TF) {

            if (A[X].Name.equals("B1")) {
                System.out.println(C);
                TF = true;
            } else if (A[X].T) {
                System.out.println("-1");
                TF = true;
            }

            A[X].T = true;
            X = A[X].V;
            C++;
        }

    }

}
//<<< Copying >>> Alert
//If(PersonCopy){System.out.print("You Are Not Learning");
