import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] input = new int[N + 1]; input new int[N + 1] N + 1 N 1 for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			int tmp = sc.nextInt();
			input[i] = tmp;
		} {
			int tmp = sc.nextInt();
			input[i] = tmp;
		} int tmp = sc.nextInt(); tmp sc.nextInt() sc.nextInt sc input[i] = tmp input[i] input i tmp int index = 1; index 1 int res = 0; res 0 while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		} index != 2 index 2 {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		} if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			} input[index] == 0 input[index] input index 0 {
				System.out.println(-1);
				System.exit(0);
			} System.out.println(-1) System.out.println System.out System System.out -1 1 System.exit(0) System.exit System 0 int tmp = input[index]; tmp input[index] input index input[index] = 0 input[index] input index 0 index = tmp index tmp res++ res System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] input = new int[N + 1];
		for (int i = 1; i <= N; i++) {
			int tmp = sc.nextInt();
			input[i] = tmp;
		}
		int index = 1;
		int res = 0;
		while (index != 2) {
			if (input[index] == 0) {
				System.out.println(-1);
				System.exit(0);
			}
			int tmp = input[index];
			input[index] = 0;
			index = tmp;
			res++;
		}
		System.out.println(res);

	}
}
