import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    }
} import java.util.HashSet; HashSet java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set 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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    }
} 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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    } 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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    } 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 next = 1; next 1 boolean can = false; can false Set<Integer> pushed = new HashSet<>(); pushed new HashSet<>() while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        } true {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        } next = button[next - 1] next button[next - 1] button next - 1 next 1 if (pushed.contains(next)) {
                break;
            } pushed.contains(next) pushed.contains pushed next {
                break;
            } break; if (next == 2) {
                can = true;
                break;
            } next == 2 next 2 {
                can = true;
                break;
            } can = true can true break; pushed.add(next) pushed.add pushed next if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        } can {
            System.out.println(pushed.size() + 1);
        } System.out.println(pushed.size() + 1) System.out.println System.out System System.out pushed.size() + 1 pushed.size() pushed.size pushed 1 {
            System.out.println(-1);
        } System.out.println(-1) System.out.println System.out System System.out -1 1 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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    }
} 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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    }
} Main
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

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 next = 1;
        boolean can = false;
        Set<Integer> pushed = new HashSet<>();
        while (true) {
            next = button[next - 1];
            if (pushed.contains(next)) {
                break;
            }

            if (next == 2) {
                can = true;
                break;
            }

            pushed.add(next);
        }

        if (can) {
            System.out.println(pushed.size() + 1);
        } else {
            System.out.println(-1);
        }
    }
}
