import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    Scanner sc = new Scanner(System.in);
    int N;
    List<List<Integer>> G;
    List<Boolean> visited;
    int INF = Integer.MAX_VALUE / 2;

    int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    }


    void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    }

    public static void main(String[] args) {
        new Main().solve();
    }
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {
    Scanner sc = new Scanner(System.in);
    int N;
    List<List<Integer>> G;
    List<Boolean> visited;
    int INF = Integer.MAX_VALUE / 2;

    int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    }


    void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    }

    public static void main(String[] args) {
        new Main().solve();
    }
} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N; N List<List<Integer>> G; G List<Boolean> visited; visited int INF = Integer.MAX_VALUE / 2; INF Integer.MAX_VALUE / 2 Integer.MAX_VALUE Integer Integer.MAX_VALUE 2 int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    } dfs {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    } if (v == 1) {
            return 0;
        } v == 1 v 1 {
            return 0;
        } return 0; 0 if (visited.get(v)) {
            return INF;
        } visited.get(v) visited.get visited v {
            return INF;
        } return INF; INF visited.set(v, true) visited.set visited v true int ret = INF; ret INF for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        } int nv G.get(v) G.get G v {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        } {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        } if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            } nv != p nv p {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            } int tmp = dfs(nv, v) + 1; tmp dfs(nv, v) + 1 dfs(nv, v) dfs nv v 1 if (ret > tmp) {
                    ret = tmp;
                } ret > tmp ret tmp {
                    ret = tmp;
                } ret = tmp ret tmp return ret; ret int v v int p p void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    } solve {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    } N = sc.nextInt() N sc.nextInt() sc.nextInt sc G = new ArrayList<>(N) G new ArrayList<>(N) visited = new ArrayList<>(N) visited new ArrayList<>(N) for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        } int i = 0; int i = 0; i 0 i < N i N ++i ++i i {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        } {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        } G.add(new ArrayList<>()) G.add G new ArrayList<>() visited.add(false) visited.add visited false for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        } int i = 0; int i = 0; i 0 i < N i N ++i ++i i {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        } {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        } int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc a-- a G.get(i).add(a) G.get(i).add G.get(i) G.get G i a int ans = dfs(0, -1); ans dfs(0, -1) dfs 0 -1 1 if (ans == INF) {
            System.out.println("-1");
            return;
        } ans == INF ans INF {
            System.out.println("-1");
            return;
        } System.out.println("-1") System.out.println System.out System System.out "-1" return; System.out.println(ans) System.out.println System.out System System.out ans public static void main(String[] args) {
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args public class Main {
    Scanner sc = new Scanner(System.in);
    int N;
    List<List<Integer>> G;
    List<Boolean> visited;
    int INF = Integer.MAX_VALUE / 2;

    int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    }


    void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    }

    public static void main(String[] args) {
        new Main().solve();
    }
} public class Main {
    Scanner sc = new Scanner(System.in);
    int N;
    List<List<Integer>> G;
    List<Boolean> visited;
    int INF = Integer.MAX_VALUE / 2;

    int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    }


    void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    }

    public static void main(String[] args) {
        new Main().solve();
    }
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    Scanner sc = new Scanner(System.in);
    int N;
    List<List<Integer>> G;
    List<Boolean> visited;
    int INF = Integer.MAX_VALUE / 2;

    int dfs(int v, int p) {
        if (v == 1) {
            return 0;
        }

        if (visited.get(v)) {
            return INF;
        }

        visited.set(v, true);

        int ret = INF;
        for (int nv : G.get(v)) {
            if (nv != p) {
                int tmp = dfs(nv, v) + 1;
                if (ret > tmp) {
                    ret = tmp;
                }
            }
        }

        return ret;
    }


    void solve() {
        N = sc.nextInt();
        G = new ArrayList<>(N);
        visited = new ArrayList<>(N);
        for (int i = 0; i < N; ++i) {
            G.add(new ArrayList<>());
            visited.add(false);
//            G.set(i , new ArrayList<>());
        }

        for (int i = 0; i < N; ++i) {
            int a = sc.nextInt();
            a--;
//            G.get(a).add(i);
            G.get(i).add(a);
        }

        int ans = dfs(0, -1);
        if (ans == INF) {
            System.out.println("-1");
            return;
        }
        System.out.println(ans);


    }

    public static void main(String[] args) {
        new Main().solve();
    }
}
