#include <bits/stdc++.h>
typedef struct {
  int e, next;
} info;
info edge[2100 * 2100];
int head[2100];
int depth[2100];
int Que[2100];
int vis[2100];
int root[2100];
int edge_num;
void add_edge(int s, int e) {
  int t = edge_num;
  edge[t].e = e;
  edge[t].next = head[s];
  head[s] = edge_num++;
}
int bfs(int s) {
  int e, next;
  int cur;
  int fr, tp;
  int res;
  Que[0] = s;
  depth[s] = 1;
  res = 1;
  for (fr = 0, tp = 1; fr != tp; fr = (fr + 1) % 2100) {
    cur = Que[fr];
    if (res < depth[cur]) res = depth[cur];
    for (e = head[cur]; e != -1; e = edge[e].next) {
      next = edge[e].e;
      depth[next] = depth[cur] + 1;
      Que[tp] = next;
      tp = (tp + 1) % 2100;
    }
  }
  return res;
}
int main() {
  int N;
  int i;
  int j;
  int m;
  int ans;
  int t;
  while (~scanf("%d", &N)) {
    edge_num = 0;
    memset(head, -1, sizeof(head));
    for (j = 0, i = 1; i <= N; i++) {
      scanf("%d", &m);
      if (m == -1) {
        root[j++] = i;
      } else
        add_edge(m, i);
    }
    ans = -1;
    for (i = 0; i < j; i++) {
      t = bfs(root[i]);
      if (ans < t) {
        ans = t;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
