import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; class TreeHeight { static class FastScanner { StringTokenizer tok = new StringTokenizer(""); BufferedReader in; FastScanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (!tok.hasMoreElements()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } } static class Node { private T key; private Node parent = null; private List> children = new ArrayList<>(); Node(T key) { this.key = key; } void addChild(Node child) { child.setParent(this); this.children.add(child); } private void setParent(Node parent) { this.parent = parent; } public Node getParent(Node parent) { return this.parent; } List> getChildren() { return children; } public T getKey() { return key; } void setKey(T key) { this.key = key; } } static int getHeight(int[] a) { Node[] nodes = new Node[a.length]; int rootIndex = 0; for (int i = 0; i < a.length; i++) { nodes[i] = new Node<>(-5); } for (int i = 0; i < a.length; i++) { int parentIndex = a[i]; if (parentIndex == -1) { rootIndex = i; nodes[i].setKey(i); } else { nodes[i].setKey(i); nodes[parentIndex].addChild(nodes[i]); } } return treeLevelCounter(nodes, rootIndex); // return treeLevelCounterRecursive(nodes[rootIndex]); } private static int treeLevelCounterRecursive(Node node) { if (node.getChildren().size() == 0) return 1; int maxCount = Integer.MIN_VALUE; int tempCount; for (int i = 0; i < node.getChildren().size(); i++) { tempCount = treeLevelCounterRecursive(node.getChildren().get(i)); if (tempCount > maxCount) maxCount = tempCount; } return 1 + maxCount; } private static int treeLevelCounter(Node[] nodes, int rootIndex) { int levelCounter = 1; Queue> q = new LinkedList<>(); int popCount = 0; int childrenCount = nodes[rootIndex].getChildren().size(); int grandChildrenCount = 0; for (int i = 0; i < childrenCount; i++) { q.add(nodes[rootIndex].getChildren().get(i)); } while (!q.isEmpty()) { if (popCount < childrenCount) { Node n = q.remove(); int i = n.getChildren().size(); if (i != 0) { for (int j = 0; j < i; j++) { q.add(n.getChildren().get(j)); } } grandChildrenCount = grandChildrenCount + i; popCount = popCount + 1; } else { popCount = 0; childrenCount = grandChildrenCount; grandChildrenCount = 0; levelCounter = levelCounter + 1; } } return levelCounter + 1; } static int getHeightNaive(int[] a) { int n = a.length; int maxHeight = 0; for (int vertex = 0; vertex < n; vertex++) { int height = 0; for (int i = vertex; i != -1; i = a[i]) height++; maxHeight = Math.max(maxHeight, height); } return maxHeight; } public static void main(String[] args) throws IOException { FastScanner scanner = new FastScanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } System.out.println(getHeight(a)); } // public static void main(String[] args) { // new Thread(null, new Runnable() { // public void run() { // try { // runTheThing(); // } catch (IOException ignored) { // } // } // }, "1", 1 << 26).start(); // // } // // private static void runTheThing() throws IOException { // FastScanner scanner = new FastScanner(System.in); // int n = scanner.nextInt(); // int[] a = new int[n]; // for (int i = 0; i < n; i++) { // a[i] = scanner.nextInt(); // } // System.out.println(getHeight(a)); // } }