#include <bits/stdc++.h>
using namespace std;
int main() {
  int node_num;
  while (scanf("%d", &node_num) == 1) {
    vector<int> G[2001];
    int a[2001];
    memset(a, 0, sizeof(a));
    int distance[2001];
    for (int i = int(1); i <= int(node_num); i++) {
      int n1 = i;
      int n2;
      scanf("%d", &n2);
      if (n2 != -1)
        G[n2].push_back(n1);
      else {
        a[n1] = -1;
        distance[n1] = 1;
      }
    }
    int maxx = 1;
    int taken[2001];
    memset(taken, 0, sizeof(taken));
    for (int i = int(1); i <= int(node_num); i++) {
      if ((a[i] == -1) && !taken[i]) {
        queue<int> q;
        q.push(i);
        taken[i] = 1;
        while (!q.empty()) {
          int u = q.front();
          q.pop();
          for (int j = int(0); j <= int(G[u].size() - 1); j++) {
            int v = G[u][j];
            if (!taken[v]) {
              taken[v] = 1;
              distance[v] = distance[u] + 1;
              maxx = max(maxx, distance[v]);
              q.push(v);
            }
          }
        }
      }
    }
    printf("%d\n", maxx);
  }
  return 0;
}
