import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(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 button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(count);
		}
	}
} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(count);
		}
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			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 button[] = new int[N]; button new int[N] N int button_check[] = new int[N]; button_check new int[N] N for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		} int i = 0 ; int i = 0 ; i 0 i < N i N i++ i++ i {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		} {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		} button[i] = sc.nextInt() button[i] button i sc.nextInt() sc.nextInt sc button_check[i] = 0 button_check[i] button_check i 0 int button_now = 0; button_now 0 int count = 0; count 0 int j = 0; j 0 int flag = 0; flag 0 while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		} button_now != 2 button_now 2 {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		} button_now = button[j] button_now button[j] button j button_check[j] = 1 button_check[j] button_check j 1 count++ count j = button_now - 1 j button_now - 1 button_now 1 if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			} button_check[j] == 1 button_check[j] button_check j 1 {
				System.out.println(-1);
				flag = 1;
				break;
			} System.out.println(-1) System.out.println System.out System System.out -1 1 flag = 1 flag 1 break; if(flag == 0) {
			System.out.println(count);
		} flag == 0 flag 0 {
			System.out.println(count);
		} System.out.println(count) System.out.println System.out System System.out count String args[] args public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(count);
		}
	}
} public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(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 button[] = new int[N];
		int button_check[] = new int[N];
		for(int i = 0 ; i < N; i++) {
			button[i] = sc.nextInt();
			button_check[i] = 0;
		}
		
		int button_now = 0;
		int count = 0;
		int j = 0;
		int flag = 0;
		
		while(button_now != 2) {
			button_now = button[j];
			button_check[j] = 1;
			count++;
			
			j = button_now - 1;
			if(button_check[j] == 1) {
				System.out.println(-1);
				flag = 1;
				break;
			}
		}
		
		if(flag == 0) {
			System.out.println(count);
		}
	}
}