import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }


	//-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); }
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.List; List java.util import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }


	//-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); }
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}

} Main public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int[] ii0 = new int[N+1]; ii0 new int[N+1] N+1 N 1 ii0[0]=0 ii0[0] ii0 0 0 for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		} int i = 1; int i = 1; i 1 i < N+1 i N+1 N 1 i++ i++ i {
			ii0[i] = Integer.parseInt(br.readLine());
		} {
			ii0[i] = Integer.parseInt(br.readLine());
		} ii0[i] = Integer.parseInt(br.readLine()) ii0[i] ii0 i Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int i=1; i 1 int count=0; count 0 while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		} i != 2 i 2 {
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		} i = ii0[i] i ii0[i] ii0 i count += 1 count 1 if(count > N+1) { System.out.println(-1); return; } count > N+1 count N+1 N 1 { System.out.println(-1); return; } System.out.println(-1) System.out.println System.out System System.out -1 1 return; System.out.println(count) System.out.println System.out System System.out count return; String[] args args //-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	} dp {
		System.out.println(s);
	} System.out.println(s) System.out.println System.out System System.out s String s s static void dp(StringBuilder s) { dp(s.toString()); } dp { dp(s.toString()); } dp(s.toString()) dp s.toString() s.toString s StringBuilder s s static void dp(int i) { dp(String.valueOf(i)); } dp { dp(String.valueOf(i)); } dp(String.valueOf(i)) dp String.valueOf(i) String.valueOf String i int i i static void dp(long i) { dp(String.valueOf(i)); } dp { dp(String.valueOf(i)); } dp(String.valueOf(i)) dp String.valueOf(i) String.valueOf String i long i i //-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); } checkInput { System.out.println(l); } System.out.println(l) System.out.println System.out System System.out l List l l //-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	} checkInput {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	} StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(float i: i1) sb.append("[" + i + "]"); float i i1 sb.append("[" + i + "]"); sb.append("[" + i + "]") sb.append sb "[" + i + "]" "[" + i + "]" "[" i "]" System.out.println(sb) System.out.println System.out System System.out sb float[] i1 i1 //-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	} checkInput {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	} StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(int i: i1) sb.append("[" + i + "]"); int i i1 sb.append("[" + i + "]"); sb.append("[" + i + "]") sb.append sb "[" + i + "]" "[" + i + "]" "[" i "]" System.out.println(sb) System.out.println System.out System System.out sb int[] i1 i1 //-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	} checkInput {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	} StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} int[] i1 i2 {
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} {
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} for(int i: i1) sb.append("[" + i + "]"); int i i1 sb.append("[" + i + "]"); sb.append("[" + i + "]") sb.append sb "[" + i + "]" "[" + i + "]" "[" i "]" sb.append(System.getProperty("line.separator")) sb.append sb System.getProperty("line.separator") System.getProperty System "line.separator" System.out.println(sb) System.out.println System.out System System.out sb int[][] i2 i2 //-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	} checkInput {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	} StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} long[] i1 i2 {
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} {
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		} for(long i: i1) sb.append("[" + i + "]"); long i i1 sb.append("[" + i + "]"); sb.append("[" + i + "]") sb.append sb "[" + i + "]" "[" + i + "]" "[" i "]" sb.append(System.getProperty("line.separator")) sb.append sb System.getProperty("line.separator") System.getProperty System "line.separator" System.out.println(sb) System.out.println System.out System System.out sb long[][] i2 i2 public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }


	//-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); }
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}

} public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }


	//-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); }
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}

} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		// 1行目個数、2行目の複数の数字を配列に格納
		//   3
		//   1 2 3
		int N = Integer.parseInt(br.readLine());
		int[] ii0 = new int[N+1];
		ii0[0]=0;
		for(int i = 1; i < N+1; i++){
			ii0[i] = Integer.parseInt(br.readLine());
		}
		

		int i=1;
		int count=0;
		while(i != 2){
			i = ii0[i];
			count += 1;
			if(count > N+1) { System.out.println(-1); return; }
		}
		


		System.out.println(count); return;
	}
	//-----------------------------------------------------------------
	// Debug.Print
	static void dp(String s) {
		System.out.println(s);
	}
	static void dp(StringBuilder s) { dp(s.toString()); }
	static void dp(int i) { dp(String.valueOf(i)); }
	static void dp(long i) { dp(String.valueOf(i)); }


	//-----------------------------------------------------------------
	// 入力値確認：リスト
	static void checkInput(List l) { System.out.println(l); }
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、float型
	//   ＜使い方＞
	//     float[] f = new float[N];
	//     checkInput(f);
	static void checkInput(float[] i1) {
		StringBuilder sb = new StringBuilder();
		for(float i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、1次元、int型
	//   ＜使い方＞
	//     int[] c = new int[N];
	//     checkInput(c);
	static void checkInput(int[] i1) {
		StringBuilder sb = new StringBuilder();
		for(int i: i1) sb.append("[" + i + "]");
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(int[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(int[] i1: i2){
			for(int i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}
	//-----------------------------------------------------------------
	// 入力値確認：配列、二次元、int型
	static void checkInput(long[][] i2) {
		StringBuilder sb = new StringBuilder();
		for(long[] i1: i2){
			for(long i: i1) sb.append("[" + i + "]");
			sb.append(System.getProperty("line.separator"));
		}
		System.out.println(sb);
	}

}
