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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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 for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       } {
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       } if(a[i] == 2){
               break;
           } a[i] == 2 a[i] a i 2 {
               break;
           } break; if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           } a[i] != 2 a[i] a i 2 {
               continue;
           } continue; if(a[N-1] != 2){
               System.out.println(-1);
               return;
           } a[N-1] != 2 a[N-1] a N-1 N 1 2 {
               System.out.println(-1);
               return;
           } System.out.println(-1) System.out.println System.out System System.out -1 1 return; int count = 1; count 1 int index = a[0]; index a[0] a 0 while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              count++;
           }           
        } true {
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              count++;
           }           
        } if(count > N){
               System.out.println(-1);
               return;
           } count > N count N {
               System.out.println(-1);
               return;
           } System.out.println(-1) System.out.println System.out System System.out -1 1 return; if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              count++;
           } index == 2 index 2 {
               System.out.println(count);
               return;
           } System.out.println(count) System.out.println System.out System System.out count return; {           
              index = a[index-1];
              count++;
           } index = a[index-1] index a[index-1] a index-1 index 1 count++ 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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              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();
        }
        
        for(int i = 0;i < N;i++){
           if(a[i] == 2){
               break;
           }            
           if(a[i] != 2){
               continue;
           }else if(a[N-1] != 2){
               System.out.println(-1);
               return;
           }
       }

        int count = 1;

        int index = a[0];
        
        while(true){
           if(count > N){
               System.out.println(-1);
               return;
           }            
           if(index == 2){
               System.out.println(count);
               return;
           }else{           
              index = a[index-1];
              count++;
           }           
        }                
       
    }        
}        