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.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author SimplyDivine
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
}
 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.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.InputStream; InputStream java.io public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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;
        FastReader in = new FastReader(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 FastReader in = new FastReader(inputStream); in new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    } TaskB public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        } solve {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        } int n = in.nextInt(); n in.nextInt() in.nextInt in int a[] = new int[n + 1]; a new int[n + 1] n + 1 n 1 for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            } int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
                a[i] = in.nextInt();
            } {
                a[i] = in.nextInt();
            } a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in boolean sw[] = new boolean[n + 1]; sw new boolean[n + 1] n + 1 n 1 sw[1] = true sw[1] sw 1 true int cnt = 0; cnt 0 boolean visited[] = new boolean[n + 1]; visited new boolean[n + 1] n + 1 n 1 int i = 1; i 1 while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            } true {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            } if (visited[i]) {
                    break;
                } visited[i] visited i {
                    break;
                } break; if (sw[2]) {
                    out.println(cnt);
                    return;
                } sw[2] sw 2 {
                    out.println(cnt);
                    return;
                } out.println(cnt) out.println out cnt return; if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                } sw[i] sw i {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                } visited[i] = true visited[i] visited i true sw[i] = false sw[i] sw i false sw[a[i]] = true sw[a[i]] sw a[i] a i true cnt++ cnt i = a[i] i a[i] a i out.println(-1) out.println out -1 1 return; int testNumber testNumber FastReader in in PrintWriter out out static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    } FastReader BufferedReader br; br StringTokenizer st; st public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        } FastReader {
            br = new BufferedReader(new
                    InputStreamReader(i));
        } br = new BufferedReader(new
                    InputStreamReader(i)) br new BufferedReader(new
                    InputStreamReader(i)) InputStream i i public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
} 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.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author SimplyDivine
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int a[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            boolean sw[] = new boolean[n + 1];
            sw[1] = true;
            int cnt = 0;
            boolean visited[] = new boolean[n + 1];
            int i = 1;
            while (true) {
                if (visited[i]) {
                    break;
                }
                if (sw[2]) {
                    out.println(cnt);
                    return;
                }
                if (sw[i]) {
                    visited[i] = true;
                    sw[i] = false;
                    sw[a[i]] = true;
                    cnt++;
                    i = a[i];
                }

            }
            out.println(-1);
            return;
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream i) {
            br = new BufferedReader(new
                    InputStreamReader(i));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
}

