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

public class Main {
	static Map<Integer,Integer> map = new HashMap<>();
	static Set<Integer> set = new HashSet<>();
	static int count = 0;
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	}
	static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	}
} import java.util.HashMap; HashMap java.util import java.util.HashSet; HashSet java.util import java.util.Map; Map java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {
	static Map<Integer,Integer> map = new HashMap<>();
	static Set<Integer> set = new HashSet<>();
	static int count = 0;
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	}
	static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	}
} Main static Map<Integer,Integer> map = new HashMap<>(); map new HashMap<>() static Set<Integer> set = new HashSet<>(); set new HashSet<>() static int count = 0; count 0 public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	} main {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	} final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			map.put(i + 1, sc.nextInt());
		} {
			map.put(i + 1, sc.nextInt());
		} map.put(i + 1, sc.nextInt()) map.put map i + 1 i 1 sc.nextInt() sc.nextInt sc solve(1) solve 1 String[] args args static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	} solve {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	} if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		} i == 2 i 2 {
			System.out.println(count);
			return;
		} System.out.println(count) System.out.println System.out System System.out count return; if(set.contains(i)){
			System.out.println(-1);
			return;
		} set.contains(i) set.contains set i {
			System.out.println(-1);
			return;
		} System.out.println(-1) System.out.println System.out System System.out -1 1 return; set.add(i) set.add set i count++ count int next = map.get(i); next map.get(i) map.get map i solve(next) solve next int i i public class Main {
	static Map<Integer,Integer> map = new HashMap<>();
	static Set<Integer> set = new HashSet<>();
	static int count = 0;
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	}
	static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	}
} public class Main {
	static Map<Integer,Integer> map = new HashMap<>();
	static Set<Integer> set = new HashSet<>();
	static int count = 0;
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	}
	static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	}
} Main
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
	static Map<Integer,Integer> map = new HashMap<>();
	static Set<Integer> set = new HashSet<>();
	static int count = 0;
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i = 0; i < N; i++) {
			map.put(i + 1, sc.nextInt());
		}
		solve(1);
	}
	static void solve(int i) {
		if(i == 2) {
			System.out.println(count);
			return;
		} else if(set.contains(i)){
			System.out.println(-1);
			return;
		}
		set.add(i);
		count++;
		int next = map.get(i);
		solve(next);
	}
}