#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10, M = 4e3 + 10, OO = 0x3f3f3f3f, mod = 1e9 + 7;
const double EPS = 1e-3;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int head[N], nxt[M], to[M], ne, vis[N], dis[N];
int n, x;
void init() {
  memset(head, -1, n * sizeof head[0]);
  ne = 0;
}
void addEdge(int f, int t) {
  to[ne] = t;
  nxt[ne] = head[f];
  head[f] = ne++;
}
void addBiEdge(int a, int b) {
  addEdge(a, b);
  addEdge(b, a);
}
int ans;
void DFS(int src, int depth) {
  ans = max(ans, depth);
  for (int k = head[src]; ~k; k = nxt[k]) {
    int v = to[k];
    DFS(v, depth + 1);
  }
}
int main() {
  scanf("%d", &n);
  init();
  for (int i = 0; i < int(n); i++) {
    scanf("%d", &x);
    if (x == -1) continue;
    --x;
    addEdge(x, i);
  }
  for (int i = 0; i < int(n); i++) DFS(i, 1);
  printf("%d\n", ans);
  return 0;
}
