import java.util.HashMap;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    }
} import java.util.HashMap; HashMap java.util import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc HashMap<Integer, Integer> a = new HashMap<>(); a new HashMap<>() for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        } Integer i=1; Integer i=1; i 1 i<=n i n i++ i++ i {            
            a.put(i, sc.nextInt());
        } {            
            a.put(i, sc.nextInt());
        } a.put(i, sc.nextInt()) a.put a i sc.nextInt() sc.nextInt sc Integer next = 1; next 1 Integer count = 0; count 0 while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        } true {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        } next = a.get(next) next a.get(next) a.get a next count++ count if (next == 1) {
                System.out.println("-1");
                return;
            } next == 1 next 1 {
                System.out.println("-1");
                return;
            } System.out.println("-1") System.out.println System.out System System.out "-1" return; if (next == 2) {
                System.out.println(count);
                return;
            } next == 2 next 2 {
                System.out.println(count);
                return;
            } System.out.println(count) System.out.println System.out System System.out count return; if (count > n) {
                System.out.println("-1");
                return;
            } count > n count n {
                System.out.println("-1");
                return;
            } System.out.println("-1") System.out.println System.out System System.out "-1" return; String[] args args public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    }
} Main
import java.util.HashMap;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> a = new HashMap<>();
        for (Integer i=1; i<=n; i++) {            
            a.put(i, sc.nextInt());
        }
        
        Integer next = 1;
        Integer count = 0;
        while (true) {
            next = a.get(next);
            count++;
            if (next == 1) {
                System.out.println("-1");
                return;
            }
            if (next == 2) {
                System.out.println(count);
                return;
            }
            if (count > n) {
                System.out.println("-1");
                return;
            } 
        }
    }
}
