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[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	}
} 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[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	} main {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] buttons = new int[n]; buttons new int[n] n for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			buttons[i] = sc.nextInt();
		} {
			buttons[i] = sc.nextInt();
		} buttons[i] = sc.nextInt() buttons[i] buttons i sc.nextInt() sc.nextInt sc sc.close() sc.close sc int glowinBtn = 1; glowinBtn 1 Set<Integer> pshdBtn = new HashSet<Integer>(); pshdBtn new HashSet<Integer>() do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn))); !(glowinBtn==2 || pshdBtn.contains(glowinBtn)) (glowinBtn==2 || pshdBtn.contains(glowinBtn)) glowinBtn==2 glowinBtn 2 pshdBtn.contains(glowinBtn) pshdBtn.contains pshdBtn glowinBtn {
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		} pshdBtn.add(glowinBtn) pshdBtn.add pshdBtn glowinBtn glowinBtn = buttons[glowinBtn-1] glowinBtn buttons[glowinBtn-1] buttons glowinBtn-1 glowinBtn 1 int result = -1; result -1 1 if(glowinBtn==2){
			result = pshdBtn.size();
		} glowinBtn==2 glowinBtn 2 {
			result = pshdBtn.size();
		} result = pshdBtn.size() result pshdBtn.size() pshdBtn.size pshdBtn System.out.print(result) System.out.print System.out System System.out result return; String[] args args public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	}
} public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		int[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	}
} 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[] buttons = new int[n];
		for(int i=0;i<n;i++){
			buttons[i] = sc.nextInt();
		}
		sc.close();
		
		int glowinBtn = 1;
		Set<Integer> pshdBtn = new HashSet<Integer>();
		do{
			pshdBtn.add(glowinBtn);
			glowinBtn = buttons[glowinBtn-1];
			
		}while(!(glowinBtn==2 || pshdBtn.contains(glowinBtn)));
		
		int result = -1;
		
		if(glowinBtn==2){
			result = pshdBtn.size();
		}
		
		System.out.print(result);
		
		return;
	}
}