
import java.util.Scanner;
public class Main {

    private static int[] button;
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    private static int[] button;
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    }
} Main private static int[] button; button private static int n; n public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    } main {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) n = sc.nextInt() n sc.nextInt() sc.nextInt sc button = new int[n+1] button new int[n+1] n+1 n 1 for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        } int i=1; int i=1; i 1 i<n+1 i n+1 n 1 i++ i++ i {
            button[i] = sc.nextInt();
        } {
            button[i] = sc.nextInt();
        } button[i] = sc.nextInt() button[i] button i sc.nextInt() sc.nextInt sc int recent = 1 recent 1 cnt = 0; cnt 0 while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        } recent != 2 recent 2 {
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        } recent = button[recent] recent button[recent] button recent cnt++ cnt if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            } cnt > n cnt n {
                System.out.println(-1);
                System.exit(0);
            } System.out.println(-1) System.out.println System.out System System.out -1 1 System.exit(0) System.exit System 0 System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args public class Main {

    private static int[] button;
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    }
} public class Main {

    private static int[] button;
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    }
} Main

import java.util.Scanner;
public class Main {

    private static int[] button;
    private static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        button = new int[n+1];
        for(int i=1; i<n+1; i++){
            button[i] = sc.nextInt();
        }

        int recent = 1, cnt = 0;
        while (recent != 2){
            recent = button[recent];
            cnt++;
            if(cnt > n){
                System.out.println(-1);
                System.exit(0);
            }
        }
        System.out.println(cnt);
    }
}
