import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	}
} import java.util.HashSet; HashSet java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {

	public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	}
} Main public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	} 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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	} 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();
		} int i = 0; int i = 0; i 0 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 sc.close() sc.close sc Set<Integer> set = new HashSet<>(); set new HashSet<>() set.add(0) set.add set 0 for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		} int i = 0; int i = 0; i 0 i++ i++ i {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		} {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		} int oldLen = set.size(); oldLen set.size() set.size set set.add(a[i]) set.add set a[i] a i i = a[i] -2 i a[i] -2 a[i] a i 2 int newLen = set.size(); newLen set.size() set.size set if(oldLen == newLen || set.contains(2)) {
				break;
			} oldLen == newLen || set.contains(2) oldLen == newLen oldLen newLen set.contains(2) set.contains set 2 {
				break;
			} break; System.out.println(set.contains(2) ? set.size()-1 : -1) System.out.println System.out System System.out set.contains(2) ? set.size()-1 : -1 set.contains(2) set.contains set 2 set.size()-1 set.size() set.size set 1 -1 1 String[] args args public class Main {

	public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	}
} public class Main {

	public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	}
} Main
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		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();
		}
		sc.close();

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for(int i = 0;;i++) {
			int oldLen = set.size();
			set.add(a[i]);
			i = a[i] -2;
			int newLen = set.size();
			if(oldLen == newLen || set.contains(2)) {
				break;
			}
		}
		System.out.println(set.contains(2) ? set.size()-1 : -1);
	}
}