import java.util.*;
// warm-up
public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	}

	public static void main(String args[]) {
		solve();
	}

} import java.util.*; util.* java public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	}

	public static void main(String args[]) {
		solve();
	}

} Main static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	} solve {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt() N sc.nextInt() sc.nextInt sc i = 1 i 1 no = N no N t = 1 t 1 j = 1; j 1 boolean ok = false; ok false int[] a = new int[N+1]; a new int[N+1] N+1 N 1 while (N-->0) a[i++] = sc.nextInt(); N-->0 N-- N 0 a[i++] = sc.nextInt() a[i++] a i++ i sc.nextInt() sc.nextInt sc while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		} no-->0 no-- no 0 {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		} if (a[j]==2) { ok = true; break; } a[j]==2 a[j] a j 2 { ok = true; break; } ok = true ok true break; j = a[j] j a[j] a j t++ t System.out.println(ok ? t : -1) System.out.println System.out System System.out ok ? t : -1 ok t -1 1 sc.close() sc.close sc public static void main(String args[]) {
		solve();
	} main {
		solve();
	} solve() solve String args[] args public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	}

	public static void main(String args[]) {
		solve();
	}

} public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	}

	public static void main(String args[]) {
		solve();
	}

} Main
import java.util.*;
// warm-up
public class Main {

	static void solve() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), i = 1, no = N, t = 1, j = 1;
		boolean ok = false;
		int[] a = new int[N+1];
		while (N-->0) a[i++] = sc.nextInt();
		while (no-->0) {
			if (a[j]==2) { ok = true; break; }
			j = a[j]; t++;
		}
		System.out.println(ok ? t : -1);
		sc.close();		
	}

	public static void main(String args[]) {
		solve();
	}

}