#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
int main() {
  scanf("%d", &n);
  a.resize(n + 1);
  int h = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int tmp, c;
  for (int j = 1; j <= n; j++) {
    tmp = 1;
    c = j;
    while (a[c] != -1) {
      c = a[c];
      tmp++;
    }
    if (tmp > h) h = tmp;
  }
  printf("%d", h);
  return 0;
}
