import java.util.*;
import static java.lang.Math.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	}
	
	public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	}
} import java.util.*; util.* java import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	}
	
	public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	} main {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
		    a[i] = sc.nextInt()-1;
		} {
		    a[i] = sc.nextInt()-1;
		} a[i] = sc.nextInt()-1 a[i] a i sc.nextInt()-1 sc.nextInt() sc.nextInt sc 1 int result = 0; result 0 result = func(a, new boolean[n], 0, 0) result func(a, new boolean[n], 0, 0) func a new boolean[n] n 0 0 System.out.println(result) System.out.println System.out System System.out result String[] args args public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	} func {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	} if(now == 1) return cnt; now == 1 now 1 return cnt; cnt if(flg[a[now]]) return -1; flg[a[now]] flg a[now] a now return -1; -1 1 flg[a[now]] = true flg[a[now]] flg a[now] a now true return func(a, flg, a[now], cnt + 1); func(a, flg, a[now], cnt + 1) func a flg a[now] a now cnt + 1 cnt 1 int[] a a boolean[] flg flg int now now int cnt cnt public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	}
	
	public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	}
	
	public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	}
} Main
import java.util.*;
import static java.lang.Math.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] a = new int[n]; 
		for(int i = 0; i < n; i++){
		    a[i] = sc.nextInt()-1;
		}
		
		
		// 計算
		int result = 0;
		result = func(a, new boolean[n], 0, 0);
		
		// 出力
		System.out.println(result);
	}
	
	public static int func(int[] a, boolean[] flg, int now, int cnt) {
	    if(now == 1) return cnt;
	    if(flg[a[now]]) return -1;
	    flg[a[now]] = true;
	    return func(a, flg, a[now], cnt + 1);
	}
}
