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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 }
} 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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 }
} 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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 } solve {
		if(i == 2) {
			System.out.println(count);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 } if(i == 2) {
			System.out.println(count);
			System.exit(0);
		} i == 2 i 2 {
			System.out.println(count);
			System.exit(0);
		} System.out.println(count) System.out.println System.out System System.out count System.exit(0) System.exit System 0 if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		} set.contains(i) set.contains set i {
			System.out.println(-1);
			System.exit(0);
		} System.out.println(-1) System.out.println System.out System System.out -1 1 System.exit(0) System.exit System 0 {
			set.add(i);
			count++;
		} set.add(i) set.add set i count++ count int a = map.get(i); a map.get(i) map.get map i solve(a) solve a 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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 }
} 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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 }
} 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);
			System.exit(0);
		}
		if(set.contains(i)) {
			System.out.println(-1);
			System.exit(0);
		} else {
			set.add(i);
			count++;
		}
		int a = map.get(i);
		solve(a);
	 }
}