import java.lang.*;
import java.io.*;
import java.util.*;


public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
}

class TaskA {
	
	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;
	
	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}
	
	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	
	public int nextInt() {
		return Integer.parseInt(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
} import java.lang.*; lang.* java import java.io.*; io.* java import java.util.*; util.* java public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
} Main public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	} main {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	} InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out InputReader in = new InputReader(inputStream); in new InputReader(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) TaskA solver = new TaskA(); solver new TaskA() solver.solve(in, out) solver.solve solver in out out.close() out.close out String[] args args class TaskA {
	
	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	}
} TaskA public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	} solve {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	} int n = in.nextInt() n in.nextInt() in.nextInt in x; x int[] a = new int[n+1]; a new int[n+1] n+1 n 1 int[] visit = new int[n+1]; visit new int[n+1] n+1 n 1 Queue<Integer> Q = new ArrayDeque<Integer>(); Q new ArrayDeque<Integer>() int ans = 0; ans 0 for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		} int i=1; int i=1; i 1 i<=n i n ++i ++i i {
			a[i] = in.nextInt();
		} {
			a[i] = in.nextInt();
		} a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in Arrays.fill(visit, -1) Arrays.fill Arrays visit -1 1 visit[1] = 0 visit[1] visit 1 0 Q.add(1) Q.add Q 1 while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		} !Q.isEmpty() Q.isEmpty() Q.isEmpty Q {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		} x = Q.poll() x Q.poll() Q.poll Q if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			} visit[a[x]] == -1 visit[a[x]] visit a[x] a x -1 1 {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			} if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				} a[x] == 2 a[x] a x 2 {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				} ans = visit[x] + 1 ans visit[x] + 1 visit[x] visit x 1 out.println(ans) out.println out ans return; visit[a[x]] = visit[x] + 1 visit[a[x]] visit a[x] a x visit[x] + 1 visit[x] visit x 1 Q.add(a[x]) Q.add Q a[x] a x out.println(-1) out.println out -1 1 InputReader in in PrintWriter out out class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;
	
	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}
	
	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	
	public int nextInt() {
		return Integer.parseInt(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
} InputReader public BufferedReader reader; reader public StringTokenizer tokenizer; tokenizer public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	} InputReader {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	} reader = new BufferedReader(new InputStreamReader(stream), 32768) reader new BufferedReader(new InputStreamReader(stream), 32768) tokenizer = null tokenizer null InputStream stream stream public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	} next {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	} while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		} tokenizer==null || !tokenizer.hasMoreTokens() tokenizer==null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		} try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			} catch (IOException e) {
				throw new RuntimeException(e);
			} IOException e {
				throw new RuntimeException(e);
			} throw new RuntimeException(e); new RuntimeException(e) {
				tokenizer = new StringTokenizer(reader.readLine());
			} tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public int nextInt() {
		return Integer.parseInt(next());
	} nextInt {
		return Integer.parseInt(next());
	} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong() {
		return Long.parseLong(next());
	} nextLong {
		return Long.parseLong(next());
	} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public double nextDouble() {
		return Double.parseDouble(next());
	} nextDouble {
		return Double.parseDouble(next());
	} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
} public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
} Main
import java.lang.*;
import java.io.*;
import java.util.*;


public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
}

class TaskA {
	
	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;
	
	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}
	
	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	
	public int nextInt() {
		return Integer.parseInt(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}