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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				break;
			}
		}
		System.out.println(ans);
	}
} 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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				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; i++)
			a[i] = Integer.parseInt(sc.next()); int i = 1; int i = 1; i 1 i <= n i n i++ i++ i 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 tem = a[1]; tem a[1] a 1 int ans = 1; ans 1 while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				break;
			}
		} tem != 2 tem 2 {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				break;
			}
		} tem = a[tem] tem a[tem] a tem ans++ ans if (ans > n) {
				ans = -1;
				break;
			} ans > n ans n {
				ans = -1;
				break;
			} ans = -1 ans -1 1 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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				break;
			}
		}
		System.out.println(ans);
	}
} Main
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; i++)
			a[i] = Integer.parseInt(sc.next());
		int tem = a[1];
		int ans = 1;
		while (tem != 2) {
			tem = a[tem];
			ans++;
			if (ans > n) {
				ans = -1;
				break;
			}
		}
		System.out.println(ans);
	}
}