
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    }
} Main public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    } main {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    } Scanner in=new Scanner(System.in); in new Scanner(System.in) int n=in.nextInt(); n in.nextInt() in.nextInt in int[]list=new int[n+1]; list new int[n+1] n+1 n 1 boolean[]isVisit=new boolean[n+1]; isVisit new boolean[n+1] n+1 n 1 for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
           list[i]= in.nextInt();
        } {
           list[i]= in.nextInt();
        } list[i]= in.nextInt() list[i] list i in.nextInt() in.nextInt in int count=0; count 0 int start=1; start 1 while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        } true {
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        } if(isVisit[start]){
            System.out.println(-1);
            return;
        } isVisit[start] isVisit start {
            System.out.println(-1);
            return;
        } System.out.println(-1) System.out.println System.out System System.out -1 1 return; isVisit[start]=true isVisit[start] isVisit start true if(start==2)
            break; start==2 start 2 break; start=list[start] start list[start] list start count++ count System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    }
} public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    }
} Main

import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[]list=new int[n+1];
        boolean[]isVisit=new boolean[n+1];
        for (int i = 1; i <= n; i++) {
           list[i]= in.nextInt();
        }
        int count=0;
        int start=1;
        
        while(true){
           if(isVisit[start]){
            System.out.println(-1);
            return;
        }
            isVisit[start]=true;
        
        if(start==2)
            break;
       
            start=list[start];
        count++;
        }
        
        
        System.out.println(count);
    }
}
