import java.util.ArrayDeque;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} import java.util.ArrayDeque; ArrayDeque java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	} main {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) StringBuilder sb = new StringBuilder(); sb new StringBuilder() int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = sc.nextInt() - 1;
		} {
			a[i] = sc.nextInt() - 1;
		} a[i] = sc.nextInt() - 1 a[i] a i sc.nextInt() - 1 sc.nextInt() sc.nextInt sc 1 boolean[] memo = new boolean[n]; memo new boolean[n] n memo[0] = true memo[0] memo 0 true ArrayDeque<Integer> deque = new ArrayDeque<>(); deque new ArrayDeque<>() deque.add(a[0]) deque.add deque a[0] a 0 int t t cnt = 1; cnt 1 boolean b = false; b false while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		} !deque.isEmpty() deque.isEmpty() deque.isEmpty deque {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		} t = deque.poll() t deque.poll() deque.poll deque if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			} t == 1 t 1 {
				b = true;
				break;
			} b = true b true break; if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			} !memo[t] memo[t] memo t {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} deque.add(a[t]) deque.add deque a[t] a t memo[t] = true memo[t] memo t true cnt++ cnt {
				break;
			} break; if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		} b {
			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);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
} Main
import java.util.ArrayDeque;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		boolean[] memo = new boolean[n];
		memo[0] = true;
		ArrayDeque<Integer> deque = new ArrayDeque<>();
		deque.add(a[0]);
		int t, cnt = 1;
		boolean b = false;
		while (!deque.isEmpty()) {
			t = deque.poll();
			if (t == 1) {
				b = true;
				break;
			} else if (!memo[t]) {
				deque.add(a[t]);
				memo[t] = true;
				cnt++;
			} else {
				break;
			}
		}
		if (b) {
			System.out.println(cnt);
		} else {
			System.out.println(-1);
		}
	}
}
