    import java.util.Scanner;
     
    class Main{
    	public static void main(String arg[]) {
    		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();
			}
    		sc.close();
    		int count = 0;
    		
    		int i = 1;
    		boolean[] isON = new boolean[N+1];
    		while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		}
    		System.out.println(count);
    	}
    } import java.util.Scanner; Scanner java.util class Main{
    	public static void main(String arg[]) {
    		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();
			}
    		sc.close();
    		int count = 0;
    		
    		int i = 1;
    		boolean[] isON = new boolean[N+1];
    		while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		}
    		System.out.println(count);
    	}
    } Main public static void main(String arg[]) {
    		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();
			}
    		sc.close();
    		int count = 0;
    		
    		int i = 1;
    		boolean[] isON = new boolean[N+1];
    		while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		}
    		System.out.println(count);
    	} 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();
			}
    		sc.close();
    		int count = 0;
    		
    		int i = 1;
    		boolean[] isON = new boolean[N+1];
    		while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		}
    		System.out.println(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+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] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc sc.close() sc.close sc int count = 0; count 0 int i = 1; i 1 boolean[] isON = new boolean[N+1]; isON new boolean[N+1] N+1 N 1 while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		} true {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		} if(a[i] == 2 ) {
    				count++;
    				break;
    			} a[i] == 2 a[i] a i 2 {
    				count++;
    				break;
    			} count++ count break; if(isON[i] == true) {
    				count = -1;
    				break;
    			} isON[i] == true isON[i] isON i true {
    				count = -1;
    				break;
    			} count = -1 count -1 1 break; count++ count isON[i] = true isON[i] isON i true i = a[i] i a[i] a i System.out.println(count) System.out.println System.out System System.out count String arg[] arg
    import java.util.Scanner;
     
    class Main{
    	public static void main(String arg[]) {
    		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();
			}
    		sc.close();
    		int count = 0;
    		
    		int i = 1;
    		boolean[] isON = new boolean[N+1];
    		while(true) {
    			if(a[i] == 2 ) {
    				count++;
    				break;
    			}
    			if(isON[i] == true) {
    				count = -1;
    				break;
    			}
    			count++;
    			isON[i] = true;
    			i = a[i];
    		}
    		System.out.println(count);
    	}
    }