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

class Main {
	static BufferedReader jvin = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter jvout = new PrintWriter(System.out);

	static int maxN = (int)1E5+5;
	int n;
	int[] a;

	public static void main(String[] args) throws IOException {
		Main instance = new Main();
		instance.passStaticVar();
	}

	public void passStaticVar() throws IOException {
		n = Integer.parseInt(jvin.readLine());
		a = new int[n+1];
		for (int i = 1; i <= n; ++i) 
			a[i] = Integer.parseInt(jvin.readLine());
		
		boolean[] marked = new boolean[n+1];
		for (int i = 1; i <= n; ++i)
			marked[i] = false;
		int result = 0, i = 1;
		for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		}
		if (i == 2) jvout.println(result);
		else jvout.println("-1");
		jvout.flush();
	}
} import java.io.*; io.* java import java.lang.*; lang.* java import java.util.*; util.* java import java.math.*; math.* java class Main {
	static BufferedReader jvin = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter jvout = new PrintWriter(System.out);

	static int maxN = (int)1E5+5;
	int n;
	int[] a;

	public static void main(String[] args) throws IOException {
		Main instance = new Main();
		instance.passStaticVar();
	}

	public void passStaticVar() throws IOException {
		n = Integer.parseInt(jvin.readLine());
		a = new int[n+1];
		for (int i = 1; i <= n; ++i) 
			a[i] = Integer.parseInt(jvin.readLine());
		
		boolean[] marked = new boolean[n+1];
		for (int i = 1; i <= n; ++i)
			marked[i] = false;
		int result = 0, i = 1;
		for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		}
		if (i == 2) jvout.println(result);
		else jvout.println("-1");
		jvout.flush();
	}
} Main static BufferedReader jvin = new BufferedReader(new InputStreamReader(System.in)); jvin new BufferedReader(new InputStreamReader(System.in)) static PrintWriter jvout = new PrintWriter(System.out); jvout new PrintWriter(System.out) static int maxN = (int)1E5+5; maxN (int)1E5+5 (int)1E5 5 int n; n int[] a; a public static void main(String[] args) throws IOException {
		Main instance = new Main();
		instance.passStaticVar();
	} main {
		Main instance = new Main();
		instance.passStaticVar();
	} Main instance = new Main(); instance new Main() instance.passStaticVar() instance.passStaticVar instance String[] args args public void passStaticVar() throws IOException {
		n = Integer.parseInt(jvin.readLine());
		a = new int[n+1];
		for (int i = 1; i <= n; ++i) 
			a[i] = Integer.parseInt(jvin.readLine());
		
		boolean[] marked = new boolean[n+1];
		for (int i = 1; i <= n; ++i)
			marked[i] = false;
		int result = 0, i = 1;
		for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		}
		if (i == 2) jvout.println(result);
		else jvout.println("-1");
		jvout.flush();
	} passStaticVar {
		n = Integer.parseInt(jvin.readLine());
		a = new int[n+1];
		for (int i = 1; i <= n; ++i) 
			a[i] = Integer.parseInt(jvin.readLine());
		
		boolean[] marked = new boolean[n+1];
		for (int i = 1; i <= n; ++i)
			marked[i] = false;
		int result = 0, i = 1;
		for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		}
		if (i == 2) jvout.println(result);
		else jvout.println("-1");
		jvout.flush();
	} n = Integer.parseInt(jvin.readLine()) n Integer.parseInt(jvin.readLine()) Integer.parseInt Integer jvin.readLine() jvin.readLine jvin 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(jvin.readLine()); int i = 1; int i = 1; i 1 i <= n i n ++i ++i i a[i] = Integer.parseInt(jvin.readLine()); a[i] = Integer.parseInt(jvin.readLine()) a[i] a i Integer.parseInt(jvin.readLine()) Integer.parseInt Integer jvin.readLine() jvin.readLine jvin boolean[] marked = new boolean[n+1]; marked new boolean[n+1] n+1 n 1 for (int i = 1; i <= n; ++i)
			marked[i] = false; int i = 1; int i = 1; i 1 i <= n i n ++i ++i i marked[i] = false; marked[i] = false marked[i] marked i false int result = 0 result 0 i = 1; i 1 for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		} ; !marked[i] && i != 2 !marked[i] marked[i] marked i i != 2 i 2 i = a[i] i = a[i] i a[i] a i {
			marked[i] = true;
			++result;
		} {
			marked[i] = true;
			++result;
		} marked[i] = true marked[i] marked i true ++result result if (i == 2) jvout.println(result);
		else jvout.println("-1"); i == 2 i 2 jvout.println(result) jvout.println jvout result jvout.println("-1") jvout.println jvout "-1" jvout.flush() jvout.flush jvout
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;

class Main {
	static BufferedReader jvin = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter jvout = new PrintWriter(System.out);

	static int maxN = (int)1E5+5;
	int n;
	int[] a;

	public static void main(String[] args) throws IOException {
		Main instance = new Main();
		instance.passStaticVar();
	}

	public void passStaticVar() throws IOException {
		n = Integer.parseInt(jvin.readLine());
		a = new int[n+1];
		for (int i = 1; i <= n; ++i) 
			a[i] = Integer.parseInt(jvin.readLine());
		
		boolean[] marked = new boolean[n+1];
		for (int i = 1; i <= n; ++i)
			marked[i] = false;
		int result = 0, i = 1;
		for ( ; !marked[i] && i != 2; i = a[i]) {
			marked[i] = true;
			++result;
		}
		if (i == 2) jvout.println(result);
		else jvout.println("-1");
		jvout.flush();
	}
}
