#include <bits/stdc++.h>
using namespace std;
int p[2020];
int f(int x, int y) {
  if (x == -1)
    return y;
  else
    return f(p[x], y + 1);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  int Max = 0;
  for (int i = 1; i <= n; i++) {
    if (f(p[i], 1) > Max) Max = f(p[i], 1);
  }
  printf("%d\n", Max);
  return 0;
}
