#include <bits/stdc++.h>
using namespace std;
int dfs(int *graph, int n) {
  int maxDepth = 0, tmp, i, j;
  for (i = 1; i <= n; i++) {
    tmp = 1, j = i;
    while (graph[j] != -1) {
      tmp++;
      j = graph[j];
    }
    maxDepth = maxDepth > tmp ? maxDepth : tmp;
  }
  return maxDepth;
}
int main() {
  int n, manager[2002];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", manager + i);
  cout << dfs(manager, n) << endl;
  return 0;
}
