#include <bits/stdc++.h>
using namespace std;
int parents[5555];
int main() {
  int N;
  memset(parents, 0xff, sizeof(parents));
  while (~scanf("%d", &N)) {
    int a;
    for (int i = 1; i <= N; i++) {
      scanf("%d", &a);
      parents[i] = a;
    }
    int res = 1, t;
    for (int i = 1; i <= N; i++) {
      if (parents[i] != -1) {
        t = 1;
        int p = i;
        while (parents[p] != -1) {
          t++;
          p = parents[p];
        }
        if (t > res) res = t;
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
