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

public class Main {
    static int[] g;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.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 {
    static int[] g;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        }
    }
} Main static int[] g; g public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.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 g = new int[n + 1] g new int[n + 1] n + 1 n 1 for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
            int a = sc.nextInt();
            g[i] = a;
        } {
            int a = sc.nextInt();
            g[i] = a;
        } int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc g[i] = a g[i] g i a Set<Integer> s = new HashSet<>(); s new HashSet<>() int current = 1; current 1 while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        } !s.contains(current) s.contains(current) s.contains s current {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        } s.add(current) s.add s current if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            } !s.contains(g[current]) s.contains(g[current]) s.contains s g[current] g current {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            } if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                } g[current] == 2 g[current] g current 2 {
                    s.add(2);
                } s.add(2) s.add s 2 {
                    current = g[current];
                } current = g[current] current g[current] g current if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        } s.contains(2) s.contains s 2 {
            System.out.println(s.size() - 1);
        } System.out.println(s.size() - 1) System.out.println System.out System System.out s.size() - 1 s.size() s.size s 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 {
    static int[] g;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        }
    }
} public class Main {
    static int[] g;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        }
    }
} Main
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    static int[] g;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while (!s.contains(current)) {
            s.add(current);
            if (!s.contains(g[current])) {
                if (g[current] == 2) {
                    s.add(2);
                } else {
                    current = g[current];
                }
            }
        }

        if (s.contains(2)) {
            System.out.println(s.size() - 1);
        } else {
            System.out.println(-1);
        }
    }
}