#include <bits/stdc++.h>
using namespace std;
vector<int> g[2000 + 10];
int f(int x) {
  int h = INT_MIN;
  for (int i = 0; i < g[x].size(); i++) {
    h = max(h, f(g[x][i]));
  }
  if (h == INT_MIN)
    return 1;
  else
    return 1 + h;
}
int main() {
  int x, n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[i] = x;
    if (x != -1) {
      g[x].push_back(i);
    }
  }
  int h = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (a[i] == -1) {
      h = max(h, f(i));
    }
  }
  cout << h;
  return 0;
}
