import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    }
} import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    } main {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    } 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[] a = new int[N]; a new int[N] N int temp = 0; temp 0 int count = 0; count 0 for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            a[i] = Integer.parseInt(sc.next());
        } {
            a[i] = Integer.parseInt(sc.next());
        } a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        } {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        } if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            } a[i] == 2 a[i] a i 2 {
                count++;
                break;
            } count++ count break; if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            } a[i] == 1 || a[i] == -1 a[i] == 1 a[i] a i 1 a[i] == -1 a[i] a i -1 1 {
                count = -1;
                break;
            } count = -1 count -1 1 break; temp = i temp i i = a[i] - 2 i a[i] - 2 a[i] a i 2 a[temp] = -1 a[temp] a temp -1 1 count++ count PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) out.println(count) out.println out count sc.close() sc.close sc out.flush() out.flush out String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    }
} Main
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] a = new int[N];
        int temp = 0;
        int count = 0;

        for (int i = 0; i < N; i++) {
            a[i] = Integer.parseInt(sc.next());
        }

        for (int i = 0; i < N; i++) {

            if (a[i] == 2) {
                count++;
                break;
            } else if (a[i] == 1 || a[i] == -1) {
                count = -1;
                break;
            }

            temp = i;
            i = a[i] - 2;
            a[temp] = -1;
            count++;
        }

        PrintWriter out = new PrintWriter(System.out);
        out.println(count);
        sc.close();
        out.flush();
    }
}