import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int[] rights = new int[n - 1]; for (int i = 0; i < n - 1; i++) { rights[i] = sc.nextInt() - 1; } Deque deq = new ArrayDeque<>(); deq.add(new Path(0, 0)); int[] costs = new int[n]; Arrays.fill(costs, Integer.MAX_VALUE); while (deq.size() > 0) { Path p = deq.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; if (p.idx > 0) { deq.addFirst(new Path(p.idx - 1, p.value)); } if (p.idx < n - 1) { deq.addLast(new Path(rights[p.idx], p.value + 1)); } } System.out.println(costs[n - 1]); } static class Path { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }