#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i;
  cin >> n;
  int p[n + 1];
  p[0] = 0;
  for (i = 1; i <= n; i++) cin >> p[i];
  int max_depth = 0;
  int depth[n + 1];
  memset(depth, 0, sizeof(depth));
  for (i = 1; i <= n; i++) {
    int j = i;
    while (p[j] != -1) {
      if (depth[j]) {
        depth[i] += depth[j];
        break;
      }
      depth[i]++;
      j = p[j];
    }
    max_depth = max(max_depth, depth[i]);
  }
  cout << max_depth + 1 << endl;
}
