import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    } main {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    } 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 boolean[] check=new boolean[N]; check new boolean[N] N for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            a[i]=sc.nextInt();
            check[i]=false;
        } {
            a[i]=sc.nextInt();
            check[i]=false;
        } a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc check[i]=false check[i] check i false int target=a[0]; target a[0] a 0 check[0]=true check[0] check 0 true int ans=1; ans 1 while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        } target!=2 target 2 {
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        } ans++ ans if(check[target-1]){
                System.out.println(-1);
                return ;
            } check[target-1] check target-1 target 1 {
                System.out.println(-1);
                return ;
            } System.out.println(-1) System.out.println System.out System System.out -1 1 return ; check[target-1]=true check[target-1] check target-1 target 1 true target=a[target-1] target a[target-1] a target-1 target 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] a=new int[N];
        boolean[] check=new boolean[N];
        
        for(int i=0;i<N;i++){
            a[i]=sc.nextInt();
            check[i]=false;
        }
        
        int target=a[0];
        check[0]=true;
        int ans=1;
        
        while(target!=2){
            ans++;
            if(check[target-1]){
                System.out.println(-1);
                return ;
            }
            check[target-1]=true;
            target=a[target-1];
        }
        
        System.out.println(ans);
    }
}
