import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    }
} Main public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    } main {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    } try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        } {
            solve(sc);
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) solve(sc) solve sc String[] args args public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    } solve {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    } int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] numbers = new int[n]; numbers new int[n] n for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            numbers[i] = sc.nextInt();
        } {
            numbers[i] = sc.nextInt();
        } numbers[i] = sc.nextInt() numbers[i] numbers i sc.nextInt() sc.nextInt sc int next = 1; next 1 int count = 0; count 0 while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        } numbers[next - 1] != 0 numbers[next - 1] numbers next - 1 next 1 0 {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        } count++ count if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            } numbers[next - 1] == 2 numbers[next - 1] numbers next - 1 next 1 2 {
                System.out.println(count);
                return;
            } System.out.println(count) System.out.println System.out System System.out count return; int tmp = next; tmp next next = numbers[next - 1] next numbers[next - 1] numbers next - 1 next 1 numbers[tmp - 1] = 0 numbers[tmp - 1] numbers tmp - 1 tmp 1 0 System.out.println(-1) System.out.println System.out System System.out -1 1 Scanner sc sc public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    }
} public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = sc.nextInt();
        }

        int next = 1;
        int count = 0;
        while (numbers[next - 1] != 0) {
            count++;
            if (numbers[next - 1] == 2) {
                System.out.println(count);
                return;
            }
            int tmp = next;
            next = numbers[next - 1];
            numbers[tmp - 1] = 0;
        }

        System.out.println(-1);
    }
}