import java.util.*;

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

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