import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] button = new int[n]; button new int[n] n for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            button[i] = Integer.parseInt(sc.next());
        } {
            button[i] = Integer.parseInt(sc.next());
        } button[i] = Integer.parseInt(sc.next()) button[i] button i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int cnt = 1; cnt 1 int next = button[0]; next button[0] button 0 for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        } {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        } if (next == 2) {
                System.out.println(cnt);
                return;
            } next == 2 next 2 {
                System.out.println(cnt);
                return;
            } System.out.println(cnt) System.out.println System.out System System.out cnt return; if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            } button[next - 1] == 2 button[next - 1] button next - 1 next 1 2 {
                cnt++;
                System.out.println(cnt);
                return;
            } cnt++ cnt System.out.println(cnt) System.out.println System.out System System.out cnt return; {
                next = button[next - 1];
                cnt++;
            } next = button[next - 1] next button[next - 1] button next - 1 next 1 cnt++ cnt 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) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] button = new int[n];
        for (int i = 0; i < n; i++) {
            button[i] = Integer.parseInt(sc.next());
        }
        int cnt = 1;
        int next = button[0];
        for (int i = 0; i < n; i++) {
            if (next == 2) {
                System.out.println(cnt);
                return;
            }
            if (button[next - 1] == 2) {
                cnt++;
                System.out.println(cnt);
                return;
            } else {
                next = button[next - 1];
                cnt++;
            }
        }
        System.out.println(-1);
    }
}
