import java.util.*;
 
public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    }
} import java.util.*; util.* java public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    }
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    } main {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] a = new int[N]; a new int[N] N Map<Integer, Integer> map = new HashMap<>(); map new HashMap<>() for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        } {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc map.put(i+1, a[i]) map.put map i+1 i 1 a[i] a i sc.close() sc.close sc int[] b = new int[N]; b new int[N] N int key = 1; key 1 int count = 0; count 0 while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        } true {
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        } if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break; b[key-1] != 1 b[key-1] b key-1 key 1 1 b[key-1] = 1 b[key-1] b key-1 key 1 1 break; key = map.get(key) key map.get(key) map.get map key count++ count if(key == 2){
                System.out.println(count);
                return;
            } key == 2 key 2 {
                System.out.println(count);
                return;
            } System.out.println(count) System.out.println System.out System System.out count return; System.out.println(-1) System.out.println System.out System System.out -1 1 String args[] args public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    }
} public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    }
} Main
import java.util.*;
 
public class Main{
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] a = new int[N];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
            map.put(i+1, a[i]);
        }
        sc.close();

        int[] b = new int[N];

        int key = 1;
        int count = 0;
        while(true){
            if(b[key-1] != 1)
                b[key-1] = 1;
            else
                break;
            key = map.get(key);
            count++;
            if(key == 2){
                System.out.println(count);
                return;
            }
        }
        System.out.println(-1);
    }
}