import java.util.*;
import java.lang.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    }
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            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[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc boolean end = false; end false int light = a[0]; light a[0] a 0 int count = 1; count 1 Set<Integer> set = new TreeSet<>(); set new TreeSet<>() set.add(1) set.add set 1 set.add(light) set.add set light while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        } true {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        } if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            } light == 2 light 2 {
                break;
            } break; if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            } set.contains(a[light - 1]) set.contains set a[light - 1] a light - 1 light 1 {
                end = true;
                break;
            } end = true end true break; {
                light = a[light - 1];
                set.add(light);
                count++;
            } light = a[light - 1] light a[light - 1] a light - 1 light 1 set.add(light) set.add set light count++ count if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        } end {
            System.out.println(-1);
        } System.out.println(-1) System.out.println System.out System System.out -1 1 {
            System.out.println(count);
        } 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[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    }
} Main
import java.util.*;
import java.lang.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        boolean end = false;
        int light = a[0];
        int count = 1;
        Set<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(light);
        while (true) {
            if (light == 2) {
                break;
            } else if(set.contains(a[light - 1])) {
                end = true;
                break;
            } else {
                light = a[light - 1];
                set.add(light);
                count++;
            }
        }
        if (end) {
            System.out.println(-1);
        } else {
            System.out.println(count);
        }
    }
}