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[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	}

} 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[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	}

} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int a[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	} main {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int a[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int a[]; a a = new int [n + 1] a new int [n + 1] n + 1 n 1 for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		} int i = 1; int i = 1; i 1 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 int idx = 1; idx 1 int cnt = 0; cnt 0 Set<Integer> set = new HashSet<Integer>(); set new HashSet<Integer>() set.add(idx) set.add set idx while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		} true {

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		} idx = a[idx] idx a[idx] a idx cnt++ cnt if (idx == 2){
				break;
			} idx == 2 idx 2 {
				break;
			} break; if (set.contains(idx)){
				cnt = -1;
				break;
			} set.contains(idx) set.contains set idx {
				cnt = -1;
				break;
			} cnt = -1 cnt -1 1 break; set.add(idx) set.add set idx System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int a[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	}

} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int a[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	}

} 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[];
		a = new int [n + 1];

		for (int i = 1; i <= n; i++){
			a[i] = sc.nextInt();
		}

		int idx = 1;
		int cnt = 0;
		Set<Integer> set = new HashSet<Integer>();
		set.add(idx);
		while (true){

			idx = a[idx];
			cnt++;

			if (idx == 2){
				break;
			}
			if (set.contains(idx)){
				cnt = -1;
				break;
			}
			set.add(idx);
		}


		System.out.println(cnt);
	}

}
