import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    }
} Main public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String line = br.readLine(); line br.readLine() br.readLine br int n = Integer.parseInt(line); n Integer.parseInt(line) Integer.parseInt Integer line int[] a = new int[n]; a new int[n] n int k = 0; k 0 int count = 1; count 1 boolean fin = false; fin false for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = Integer.parseInt(br.readLine());
        } {
            a[i] = Integer.parseInt(br.readLine());
        } a[i] = Integer.parseInt(br.readLine()) a[i] a i Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        } {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        } if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            } a[k] != 2 && !fin a[k] != 2 a[k] a k 2 !fin fin {
                count++;
                k = a[k] - 1;
            } count++ count k = a[k] - 1 k a[k] - 1 a[k] a k 1 {
                fin = true;
            } fin = true fin true if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        } fin {
            System.out.println(count);
        } System.out.println(count) System.out.println System.out System System.out count {
            System.out.println("-1");
        } System.out.println("-1") System.out.println System.out System System.out "-1" String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(line);
        int[] a = new int[n];
        int k = 0;
        int count = 1;
        boolean fin = false;
        for(int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }
        for(int i = 0; i < n; i++) {
            if(a[k] != 2 && !fin) {
                count++;
                k = a[k] - 1;
            } else {
                fin = true;
            }
        }
        if(fin) {
            System.out.println(count);
        } else {
            System.out.println("-1");
        }
    }
}