import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util import java.io.IOException; IOException java.io import java.util.InputMismatchException; InputMismatchException java.util import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.TreeSet; TreeSet java.util import java.io.InputStream; InputStream java.io public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out InputReader in = new InputReader(inputStream); in new InputReader(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) TaskB solver = new TaskB(); solver new TaskB() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    } TaskB public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        } solve {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        } int n = in.nextInt(); n in.nextInt() in.nextInt in int[] a = in.nextIntArray(n); a in.nextIntArray(n) in.nextIntArray in n TreeSet<Integer> set = new TreeSet<>(); set new TreeSet<>() set.add(1) set.add set 1 int now = 1; now 1 for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            } {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            } now = a[now - 1] now a[now - 1] a now - 1 now 1 if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                } now == 2 now 2 {
                    out.println(set.size());
                    return;
                } out.println(set.size()) out.println out set.size() set.size set return; if (set.contains(now)) {
                    out.println(-1);
                    return;
                } set.contains(now) set.contains set now {
                    out.println(-1);
                    return;
                } out.println(-1) out.println out -1 1 return; set.add(now) set.add set now int testNumber testNumber InputReader in in PrintWriter out out static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    } InputReader BufferedReader in; in StringTokenizer tok; tok public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        } nextString {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        } while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            } !tok.hasMoreTokens() tok.hasMoreTokens() tok.hasMoreTokens tok {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            } try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                } catch (IOException e) {
                    throw new InputMismatchException();
                } IOException e {
                    throw new InputMismatchException();
                } throw new InputMismatchException(); new InputMismatchException() {
                    tok = new StringTokenizer(in.readLine(), " ");
                } tok = new StringTokenizer(in.readLine(), " ") tok new StringTokenizer(in.readLine(), " ") return tok.nextToken(); tok.nextToken() tok.nextToken tok public int nextInt() {
            return Integer.parseInt(nextString());
        } nextInt {
            return Integer.parseInt(nextString());
        } return Integer.parseInt(nextString()); Integer.parseInt(nextString()) Integer.parseInt Integer nextString() nextString public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        } nextIntArray {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        } int[] res = new int[n]; res new int[n] n for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                res[i] = nextInt();
            } {
                res[i] = nextInt();
            } res[i] = nextInt() res[i] res i nextInt() nextInt return res; res int n n public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        } InputReader {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        } in = new BufferedReader(new InputStreamReader(inputStream)) in new BufferedReader(new InputStreamReader(inputStream)) tok = new StringTokenizer("") tok new StringTokenizer("") InputStream inputStream inputStream public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);

            TreeSet<Integer> set = new TreeSet<>();
            set.add(1);
            int now = 1;
            for (int i = 0; i < n; i++) {
                now = a[now - 1];
                if (now == 2) {
                    out.println(set.size());
                    return;
                } else if (set.contains(now)) {
                    out.println(-1);
                    return;
                }
                set.add(now);
            }
        }

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer tok;

        public String nextString() {
            while (!tok.hasMoreTokens()) {
                try {
                    tok = new StringTokenizer(in.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return tok.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public int[] nextIntArray(int n) {
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        public InputReader(InputStream inputStream) {
            in = new BufferedReader(new InputStreamReader(inputStream));
            tok = new StringTokenizer("");
        }

    }
}

