import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    }
} Main public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    } main {
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    } Scanner scan=new Scanner(System.in); scan new Scanner(System.in) int N=scan.nextInt(); N scan.nextInt() scan.nextInt scan int[] a=new int[N]; a new int[N] N int[] judge=new int[N]; judge new int[N] N int i; i int flag=0; flag 0 int count=0; count 0 for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        } i=0; i=0 i 0 i<N i N i++ i++ i {
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        } {
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        } a[i]=scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan judge[i]=0 judge[i] judge i 0 if(a[i]==2){
                flag++;
            } a[i]==2 a[i] a i 2 {
                flag++;
            } flag++ flag if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        } flag==0 flag 0 {
            System.out.println("-1");
            return;
        } System.out.println("-1") System.out.println System.out System System.out "-1" return; {
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        } int j=0 j 0 light=1; light 1 while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            } light!=2 light 2 {
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            } judge[j]++ judge[j] judge j if(judge[j]>1){
                    System.out.println("-1");
                    return;
                } judge[j]>1 judge[j] judge j 1 {
                    System.out.println("-1");
                    return;
                } System.out.println("-1") System.out.println System.out System System.out "-1" return; count++ count light=a[j] light a[j] a j j=light-1 j light-1 light 1 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 scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    }
} public class Main{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    }
} Main
import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] a=new int[N];
        int[] judge=new int[N];
        int i;
        int flag=0;
        int count=0;
        for(i=0;i<N;i++){
            a[i]=scan.nextInt();
            judge[i]=0;
            if(a[i]==2){
                flag++;
            }
        }
        if(flag==0){
            System.out.println("-1");
            return;
        }
        else{
            int j=0,light=1;
            while(light!=2){
                judge[j]++;
                if(judge[j]>1){
                    System.out.println("-1");
                    return;
                }
                count++;
                light=a[j];
                j=light-1;
            }
            System.out.println(count);
        }
    }
}