#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 10;
int top, indegree[maxn], n, money[maxn];
struct Edge {
  int to;
  Edge* next;
} * head[maxn], e[maxn];
void Addedge(int from, int to) {
  Edge* p = &e[top++];
  p->to = to, p->next = head[from], head[from] = p;
}
void Topsort() {
  queue<int> Q;
  int k = 1, i;
  for (i = 1; i <= n; i++)
    if (!indegree[i]) {
      Q.push(i);
      money[i] = 1;
    }
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    for (Edge* p = head[x]; p; p = p->next) {
      indegree[p->to]--;
      if (!indegree[p->to]) {
        money[p->to] = money[x] + 1;
        Q.push(p->to);
      }
    }
  }
}
int main() {
  int i, ans, a;
  while (~scanf("%d", &n)) {
    ans = top = 0;
    memset(head, 0, sizeof(head));
    for (i = 1; i <= n; i++) {
      scanf("%d", &a);
      if (a != -1) Addedge(a, i), indegree[i]++;
    }
    Topsort();
    for (i = 1; i <= n; i++) ans = max(ans, money[i]);
    printf("%d\n", ans);
  }
  return 0;
}
