#include <bits/stdc++.h>
using namespace std;
int n;
int p[100000];
int dfs(int u) {
  if (u == -1) return 0;
  return 1 + dfs(p[u]);
}
int main() {
  srand(1543151);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  int best = 0;
  for (int i = 1; i <= n; i++) best = max(best, dfs(i));
  printf("%d\n", best);
  return 0;
}
