#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cout << setprecision(12);
  int n;
  cin >> n;
  ;
  vector<int> p(n);
  for (int i = 0; i < n; i++) cin >> p[i];
  ;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) p[i]--;
  }
  vector<int> h(n, -1);
  for (int i = 0; i < n; i++) {
    stack<pair<int, bool>> s;
    s.push(make_pair(i, false));
    while (!s.empty()) {
      auto t = s.top();
      s.pop();
      int v = t.first;
      bool finished = t.second;
      if (h[v] != -1)
        continue;
      else if (p[v] == -1)
        h[v] = 1;
      else if (finished)
        h[v] = h[p[v]] + 1;
      else {
        s.push(make_pair(v, true));
        s.push(make_pair(p[v], false));
      }
    }
  }
  int result = 1;
  for (int i = 0; i < n; i++) {
    result = max(result, h[i]);
  }
  cout << result << endl;
}
