#include <bits/stdc++.h>
using namespace std;
const long long N = 2005, M = 50005, OO = 0x3f3f3f3f, mod = 1e9 + 7;
const double EPS = 1e-3;
int n, head[N], nxt[M], to[M], ne, x;
int dis[N];
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 BFS(int src, int len) {
  queue<int> q;
  if (len >= dis[src]) dis[src] = len;
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int k = head[u]; ~k; k = nxt[k]) {
      int v = to[k];
      dis[v] = dis[u] + 1;
      q.push(v);
    }
  }
}
int main() {
  scanf("%d", &n);
  init();
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    if (x == -1) continue;
    --x;
    addEdge(x, i);
  }
  fill(dis, dis + n, 1);
  for (int i = 0; i < n; ++i) {
    BFS(i, 1);
  }
  sort(dis, dis + n);
  printf("%d\n", dis[n - 1]);
  return 0;
}
