

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    } main {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    } int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n + 1]; a new int[n + 1] n + 1 n 1 for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        } int i = 1; int i = 1; i 1 i < n + 1 i n + 1 n 1 i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int slow = a[1]; slow a[1] a 1 boolean[] checked = new boolean[n + 1]; checked new boolean[n + 1] n + 1 n 1 int count = 1; count 1 while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        } !checked[slow] checked[slow] checked slow {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        } if (slow == 2) {
                System.out.println(count);
                return;
            } slow == 2 slow 2 {
                System.out.println(count);
                return;
            } System.out.println(count) System.out.println System.out System System.out count return; count++ count checked[slow] = true checked[slow] checked slow true slow = a[slow] slow a[slow] a slow System.out.println(-1) System.out.println System.out System System.out -1 1 String[] args args public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    }
} public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    }
} Main


import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            a[i] = sc.nextInt();
        }
        int slow = a[1];
        boolean[] checked = new boolean[n + 1];
        int count = 1;
        while (!checked[slow]) {
            if (slow == 2) {
                System.out.println(count);
                return;
            }
            count++;
            checked[slow] = true;
            slow = a[slow];
        }
        System.out.println(-1);
    }
}
