import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n n a[] a key = -1; key -1 1 n = sc.nextInt() n sc.nextInt() sc.nextInt sc a = new int[n + 1] a new int[n + 1] n + 1 n 1 for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		} int i = 1; int i = 1; i 1 i < n + 1 i n + 1 n 1 ++i ++i i {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		} {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc if(a[i] == 2)key = i; a[i] == 2 a[i] a i 2 key = i key i sc.close() sc.close sc int ans = -1; ans -1 1 boolean light[]; light light = new boolean[n + 1] light new boolean[n + 1] n + 1 n 1 if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		} key == 1 key 1 {
			ans = 1;
		} ans = 1 ans 1 if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		} key != -1 key -1 1 {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		} ans = 0 ans 0 int b = 1; b 1 light[1] = true light[1] light 1 true while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			} true {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			} b = a[b] b a[b] a b ++ans ans if(b == 2) break;//present light == 2 b == 2 b 2 break;//present light == 2 if(light[b]) {
					ans = -1;
					break;
				} light[b] light b {
					ans = -1;
					break;
				} ans = -1 ans -1 1 break; light[b] = true light[b] light b true 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) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n, a[], key = -1;
		
		n = sc.nextInt();
		
		a = new int[n + 1];
		
		for(int i = 1; i < n + 1; ++i) {
			a[i] = sc.nextInt();
			if(a[i] == 2)key = i;
		}
		sc.close();
		
		int ans = -1;
		
		boolean light[];
		light = new boolean[n + 1];//have already lighted
		
		if(key == 1) {
			ans = 1;
		}
		else if(key != -1) {
			ans = 0;
			int b = 1;//first light == 1
			light[1] = true;
			
			while(true) {
				b = a[b];//push button No.b & present light == b
				++ans;//counting
				
				if(b == 2) break;//present light == 2
				if(light[b]) {
					ans = -1;
					break;
				}
				light[b] = true;
			}
			
		}
		System.out.println(ans);
	}

}
