

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 = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	}

} 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 = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);

		int n = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] a = new int[n+1]; a new int[n+1] n+1 n 1 for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		} int i = 1; int i = 1; i 1 i < n+1 i n+1 n 1 i++ i++ i {
			a[i] = Integer.parseInt(sc.next());
		} {
			a[i] = Integer.parseInt(sc.next());
		} a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int b = 1; b 1 int ans = -1; ans -1 1 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map new HashMap<Integer, Integer>() map.put(1, 1) map.put map 1 1 for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		} int i = 1; int i = 1; i 1 i < n + 1 i n + 1 n 1 i++ i++ i {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		} {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		} b = a[b] b a[b] a b if (b == 2) {
				ans = i;
				break;
			} b == 2 b 2 {
				ans = i;
				break;
			} ans = i ans i break; if (map.containsKey(b)) {
				break;
			} map.containsKey(b) map.containsKey map b {
				break;
			} break; System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	}

} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	}

} 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 = Integer.parseInt(sc.next());
		int[] a = new int[n+1];

		for (int i = 1; i < n+1; i++) {
			a[i] = Integer.parseInt(sc.next());
		}

		int b = 1;
		int ans = -1;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		map.put(1, 1);

		for (int i = 1; i < n + 1; i++) {
			b = a[b];

			if (b == 2) {
				ans = i;
				break;
			}

			if (map.containsKey(b)) {
				break;
			}
		}

		System.out.println(ans);
	}

}
