import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} import java.util.HashMap; HashMap java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc HashMap<Integer, Integer> map = new HashMap<>(); map new HashMap<>() for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			map.put(i, sc.nextInt());
		} {
			map.put(i, sc.nextInt());
		} map.put(i, sc.nextInt()) map.put map i sc.nextInt() sc.nextInt sc int cnt = 0; cnt 0 boolean can = false; can false for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		} int i = 1; int i = 1; i 1 cnt < N cnt N {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		} {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		} cnt++ cnt i = map.get(i) i map.get(i) map.get map i if (i ==  2) {
				can = true;
				break;
			} i ==  2 i 2 {
				can = true;
				break;
			} can = true can true break; if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		} can {
			System.out.println(cnt);
		} System.out.println(cnt) System.out.println System.out System System.out cnt {
			System.out.println(-1);
		} System.out.println(-1) System.out.println System.out System System.out -1 1 String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} Main
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 1; i <= N; i++) {
			map.put(i, sc.nextInt());
		}
		int cnt = 0;
		boolean can = false;
		for (int i = 1; cnt < N;) {
			cnt++;
			i = map.get(i);
			if (i ==  2) {
				can = true;
				break;
			}
		}
		if (can) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
}
