import java.util.*;

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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(count);
        }
    }
} import java.util.*; util.* 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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(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 int[] copy = new int[n]; copy new int[n] n for(int i = 0; i < n; i++){
            copy[i] = a[i];
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            copy[i] = a[i];
        } {
            copy[i] = a[i];
        } copy[i] = a[i] copy[i] copy i a[i] a i int count = 0; count 0 int light = 1; light 1 int flag = 1; flag 1 for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        } int i = 0; int i = 0; i 0 i++ i++ i {
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        } {
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        } if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            } light != 2 && copy[light-1] != 0 light != 2 light 2 copy[light-1] != 0 copy[light-1] copy light-1 light 1 0 {
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } copy[light-1] = 0 copy[light-1] copy light-1 light 1 0 light = a[light-1] light a[light-1] a light-1 light 1 count++ count if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            } copy[light-1] == 0 copy[light-1] copy light-1 light 1 0 {
                flag = 0;
                break;
            } flag = 0 flag 0 break; if(light == 2){
                break;
            } light == 2 light 2 {
                break;
            } break; if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(count);
        } flag == 0 flag 0 {
            System.out.print("-1");
        } System.out.print("-1") System.out.print System.out System System.out "-1" {
            System.out.print(count);
        } System.out.print(count) System.out.print 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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(count);
        }
    }
} Main
import java.util.*;

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();
        }
        int[] copy = new int[n];
        for(int i = 0; i < n; i++){
            copy[i] = a[i];
        }
        int count = 0;
        int light = 1;
        int flag = 1;
        for(int i = 0; ; i++){
            if(light != 2 && copy[light-1] != 0){
                copy[light-1] = 0;
                light = a[light-1];
                count++;
            } else if(copy[light-1] == 0){
                flag = 0;
                break;
            } else if(light == 2){
                break;
            }
        }
        if(flag == 0){
            System.out.print("-1");
        } else {
            System.out.print(count);
        }
    }
}