import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                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 total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    } main {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int total = sc.nextInt(); total sc.nextInt() sc.nextInt sc int[] buttons = new int[total + 1]; buttons new int[total + 1] total + 1 total 1 for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        } int i = 1; int i = 1; i 1 i <= total i total i++ i++ i {
            buttons[i] = sc.nextInt();
        } {
            buttons[i] = sc.nextInt();
        } buttons[i] = sc.nextInt() buttons[i] buttons i sc.nextInt() sc.nextInt sc int num = 1; num 1 int i = 0; i 0 do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total); i <= total i total {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } num = buttons[num] num buttons[num] buttons num i++ i if (num == 2){
                        break;
                } num == 2 num 2 {
                        break;
                } break; if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        } num == 2 num 2 {
                System.out.println(i);
        } System.out.println(i) System.out.println System.out System System.out i {
                System.out.println("-1");
        } 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 total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                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 total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    }
}