import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	}
} Main public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	} main {
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	} boolean isTwo = false; isTwo false int tmp=0; tmp 0 Scanner in = new java.util.Scanner(System.in); in new java.util.Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int[] array = new int[N]; array new int[N] N boolean[] flags = new boolean[N]; flags new boolean[N] N for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		} int i=0; int i=0; i 0 i<array.length i array.length array array.length i++ i++ i {
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		} {
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		} tmp = in.nextInt() tmp in.nextInt() in.nextInt in if(tmp == 2){
				isTwo = true;
			} tmp == 2 tmp 2 {
				isTwo = true;
			} isTwo = true isTwo true array[i] = tmp array[i] array i tmp if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		} isTwo == false isTwo false {
			System.out.print("-1");
			System.exit(0);
		} System.out.print("-1") System.out.print System.out System System.out "-1" System.exit(0) System.exit System 0 int count = 0; count 0 count++ count int now = 1; now 1 while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		} array[now-1] != 2 array[now-1] array now-1 now 1 2 {
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		} if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			} flags[now-1] == true flags[now-1] flags now-1 now 1 true {
				System.out.print("-1");
				System.exit(0);
			} System.out.print("-1") System.out.print System.out System System.out "-1" System.exit(0) System.exit System 0 flags[now-1] = true flags[now-1] flags now-1 now 1 true now = array[now -1] now array[now -1] array now -1 now 1 count++ count System.out.print(count) System.out.print System.out System System.out count String[] args args public class Main {
	public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	}
} public class Main {
	public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		boolean isTwo = false;
		int tmp=0;
		Scanner in = new java.util.Scanner(System.in);
		int N = in.nextInt();
		int[] array = new int[N];
		boolean[] flags = new boolean[N];
		for(int i=0; i<array.length; i++){
			tmp = in.nextInt();
			if(tmp == 2){
				isTwo = true;
			}
			array[i] = tmp;
		}
		if(isTwo == false){
			System.out.print("-1");
			System.exit(0);
		}
		int count = 0;
		count++;
		int now = 1;
		while(array[now-1] != 2){
			if(flags[now-1] == true){
				System.out.print("-1");
				System.exit(0);
			}
			flags[now-1] = true;
			now = array[now -1];
			count++;
		}
		System.out.print(count);
	}
}
