import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    }
} import java.util.*; util.* java public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] button = new int[n]; button new int[n] n for (int i = 0; i < n; i++) {
            button[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n 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 count = 0; count 0 int nextButtonIndex = 0; nextButtonIndex 0 while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        } n > 0 n 0 {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        } count++ count n-- n if (button[nextButtonIndex] == 2) break; button[nextButtonIndex] == 2 button[nextButtonIndex] button nextButtonIndex 2 break; nextButtonIndex = button[nextButtonIndex]-1 nextButtonIndex button[nextButtonIndex]-1 button[nextButtonIndex] button nextButtonIndex 1 if (n == 0 && button[nextButtonIndex] != 2) count = -1; n == 0 && button[nextButtonIndex] != 2 n == 0 n 0 button[nextButtonIndex] != 2 button[nextButtonIndex] button nextButtonIndex 2 count = -1 count -1 1 System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    }
} Main
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// ボタンの数
        int[] button = new int[n];

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

        int count = 0;
        int nextButtonIndex = 0;
        while (n > 0) {
            count++;// ボタンをおす
            n--;
            if (button[nextButtonIndex] == 2) break;

            // 次のボタンへ遷移
            nextButtonIndex = button[nextButtonIndex]-1;
        }

        if (n == 0 && button[nextButtonIndex] != 2) count = -1;

        System.out.println(count);
    }
}
