/* package whatever; // don't place package name! */

import java.util.*;
public class Main{
	static class Node{
		Node right;
		Node left;
		int data;
		Node(int data){
			this.data = data;
		}
	}
	static Node BuildTree(int[]vals){
		if(vals.length == 0 || vals[0] == -1) return null;
		Node root = new Node(vals[0]);
		Queue<Node> q = new LinkedList<>();
		q.offer(root);
		int i = 1;
		while(!q.isEmpty() && i<vals.length){
			Node curr = q.poll();
			if(i<vals.length && vals[i]!= -1){
				curr.left =new Node(vals[i]);
				q.offer(curr.left);
			}i++;
			if(i<vals.length && vals[i]!= -1){
				curr.right =new Node(vals[i]);
				q.offer(curr.right);
			}i++;
		}
		return root;
	}
	static int height(Node root){
		if(root == null) return -1;
		int left = height(root.left);
		int right = height(root.right);
		
		return 1 + Math.max(left,right);
	}
	public static void main(String[]args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0; i<n; i++){
			arr[i] = sc.nextInt();
		}
		Node root = BuildTree(arr);
		
		System.out.println(height(root));
	}
}