import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	}

	static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	}

	static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	} main {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) ArrayList<Integer> num = new ArrayList<Integer>(); num new ArrayList<Integer>() ArrayList<String> param = new ArrayList<String>(); param new ArrayList<String>() int system = 0; system 0 while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		} sc.hasNext() sc.hasNext sc {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		} if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			} sc.hasNextInt() sc.hasNextInt sc {
				num.add(sc.nextInt());
			} num.add(sc.nextInt()) num.add num sc.nextInt() sc.nextInt sc {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			} if(system==0) param.add(sc.next()); system==0 system 0 param.add(sc.next()) param.add param sc.next() sc.next sc if(system==1) param.add(sc.nextLine()); system==1 system 1 param.add(sc.nextLine()) param.add param sc.nextLine() sc.nextLine sc Method(num, param) Method num param String[] args args static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	} Method {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	} int now = 1; now 1 int count = 0; count 0 while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		} true {
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		} now = num.get(now) now num.get(now) num.get num now count++ count if(now == 2) break; now == 2 now 2 break; if(count == num.size()){
				count = -1;
				break;
			} count == num.size() count num.size() num.size num {
				count = -1;
				break;
			} count = -1 count -1 1 break; System.out.println(count) System.out.println System.out System System.out count ArrayList<Integer> num num ArrayList<String> param param public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	}

	static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	}

	static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	}
} Main
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> num = new ArrayList<Integer>();
		ArrayList<String> param = new ArrayList<String>();
		int system = 0; // 文字の空白区切り　0:ON　1:OFF

		while (sc.hasNext()) {
			if (sc.hasNextInt()) {
				num.add(sc.nextInt());
			} else {
				if(system==0) param.add(sc.next());
				if(system==1) param.add(sc.nextLine());
			}
		}
		Method(num, param);
	}

	static void Method(ArrayList<Integer> num, ArrayList<String> param) {
		
		int now = 1;
		int count = 0;
		
		while(true){
			now = num.get(now);
			count++;
			if(now == 2) break;
			if(count == num.size()){
				count = -1;
				break;
			}
		}
		
		System.out.println(count);
	}
}