#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, m = 0;
  cin >> n;
  int arr[n], a[n];
  stack<int> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == -1) {
      s.push(i + 1);
      a[i] = 0;
    }
  }
  while (!s.empty()) {
    x = s.top();
    s.pop();
    for (int i = 0; i < n; i++) {
      if (arr[i] == x) {
        s.push(i + 1);
        a[i] = a[x - 1] + 1;
        if (m < a[i]) m = a[i];
      }
    }
  }
  cout << m + 1;
}
