import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    }
} import java.util.HashSet; HashSet java.util import java.util.LinkedList; LinkedList java.util import java.util.Queue; Queue java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {
    public static void main(String[] args) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    }
} Main public static void main(String[] args) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    } main {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    } int N; N int[] Ai; Ai Set<Integer> searched = new HashSet<>(); searched new HashSet<>() Queue<Integer> queue = new LinkedList<>(); queue new LinkedList<>() try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        } {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) N = sc.nextInt() N sc.nextInt() sc.nextInt sc Ai = new int[N] Ai new int[N] N for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
                Ai[i] = sc.nextInt();
            } {
                Ai[i] = sc.nextInt();
            } Ai[i] = sc.nextInt() Ai[i] Ai i sc.nextInt() sc.nextInt sc queue.add(0) queue.add queue 0 searched.add(0) searched.add searched 0 while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        } !queue.isEmpty() queue.isEmpty() queue.isEmpty queue {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        } Integer i = queue.poll(); i queue.poll() queue.poll queue if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            } i == 1 i 1 {
                System.out.println(searched.size() - 1);
                return;
            } System.out.println(searched.size() - 1) System.out.println System.out System System.out searched.size() - 1 searched.size() searched.size searched 1 return; Integer t = Ai[i] - 1; t Ai[i] - 1 Ai[i] Ai i 1 if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            } !searched.contains(t) searched.contains(t) searched.contains searched t {
                queue.add(t);
                searched.add(t);
            } queue.add(t) queue.add queue t searched.add(t) searched.add searched t 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) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    }
} public class Main {
    public static void main(String[] args) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    }
} Main
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {

        int N;
        int[] Ai;
        Set<Integer> searched = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextInt();
            Ai = new int[N];
            for (int i = 0; i < N; i++) {
                Ai[i] = sc.nextInt();
            }
        }
        queue.add(0);
        searched.add(0);

        while (!queue.isEmpty()) {
            Integer i = queue.poll();
            if (i == 1) {
                System.out.println(searched.size() - 1);
                return;
            }
            Integer t = Ai[i] - 1;
            if (!searched.contains(t)) {
                queue.add(t);
                searched.add(t);
            }
        }
        System.out.println(-1);
    }
}
