import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int[] b = new int [a+1]; b new int [a+1] a+1 a 1 for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        } int i=1; int i=1; i 1 i<=a i a i++ i++ i {
            b[i] = sc.nextInt();
        } {
            b[i] = sc.nextInt();
        } b[i] = sc.nextInt() b[i] b i sc.nextInt() sc.nextInt sc int next = 1; next 1 for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        } int i=1; int i=1; i 1 i<=1000000 i 1000000 i++ i++ i {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        } {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        } if(b[next] == 2) {
                System.out.println(i);
                return;
            } b[next] == 2 b[next] b next 2 {
                System.out.println(i);
                return;
            } System.out.println(i) System.out.println System.out System System.out i return; next = b[next] next b[next] b next System.out.println("-1") System.out.println System.out System System.out "-1" String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] b = new int [a+1];
        for(int i=1; i<=a; i++) {
            b[i] = sc.nextInt();
        }
        int next = 1;
        for(int i=1; i<=1000000; i++) {
            if(b[next] == 2) {
                System.out.println(i);
                return;
            }
            next = b[next];
        }
        System.out.println("-1");
    }
}