import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

} import java.io.InputStream; InputStream java.io import java.io.PrintStream; PrintStream java.io import java.util.Scanner; Scanner java.util public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

} Main InputStream in = System.in; in System.in System System.in PrintStream out = System.out; out System.out System System.out public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	} _main {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	} Scanner sc = new Scanner(in); sc new Scanner(in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt 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] = sc.nextInt();
		} int i=1; int i=1; i 1 i<=N i N i++ i++ i {
			a[i] = sc.nextInt();
		} {
			a[i] = sc.nextInt();
		} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc boolean[] visited = new boolean[N+1]; visited new boolean[N+1] N+1 N 1 sc.close() sc.close sc int p=1; p 1 int ans = 0; ans 0 while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		} true {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		} if(p==2) {
				break;
			} p==2 p 2 {
				break;
			} break; if(visited[a[p]]) {
				ans = -1;
				break;
			} visited[a[p]] visited a[p] a p {
				ans = -1;
				break;
			} ans = -1 ans -1 1 break; p = a[p] p a[p] a p visited[p] = true visited[p] visited p true ans++ ans out.println(ans) out.println out ans String[] args args public static void main(String[] args) {
		new Main()._main(args);
	} main {
		new Main()._main(args);
	} new Main()._main(args) new Main()._main new Main() args String[] args args public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

} public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

} Main
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	InputStream in = System.in;
	PrintStream out = System.out;

	public void _main(String[] args) {
		Scanner sc = new Scanner(in);

		int N = sc.nextInt();
		int[] a = new int[N+1];
		for(int i=1;i<=N;i++) {
			a[i] = sc.nextInt();
		}

		boolean[] visited = new boolean[N+1];


		sc.close();

		int p=1;
		int ans = 0;
		while(true) {
			if(p==2) {
				break;
			}

			if(visited[a[p]]) {
				ans = -1;
				break;
			}
			p = a[p];
			visited[p] = true;
			ans++;

		}
		out.println(ans);

	}

	public static void main(String[] args) {
		new Main()._main(args);
	}

}
