#include <bits/stdc++.h>
using namespace std;
int graph[2001][2001] = {0};
int emp, tmp = 0, max1 = 1;
int indegree[2001] = {0};
int mark[2001] = {0};
int bfs(int i) {
  queue<int> Q;
  int level[2001] = {0};
  level[i] = 1;
  Q.push(i);
  while (Q.size()) {
    int tmp = Q.front();
    Q.pop();
    for (int a = 1; a <= emp; a++) {
      if (graph[tmp][a] && !mark[a]) {
        mark[a] = 1;
        Q.push(a);
        level[a] = level[tmp] + 1;
      }
    }
  }
  int maximum = 1;
  for (int i = 0; i <= emp; i++) {
    if (maximum < level[i]) maximum = level[i];
  }
  return maximum;
}
int main() {
  int degree0[2001] = {0};
  cin >> emp;
  for (int i = 1; i <= emp; i++) {
    int h;
    cin >> h;
    if (h != -1) {
      graph[h][i] = 1;
      indegree[i]++;
    } else {
      degree0[i] = 1;
    }
  }
  for (int i = 1; i <= emp; i++) {
    if (degree0[i]) {
      tmp = bfs(i);
      if (tmp > max1) max1 = tmp;
    }
  }
  cout << max1 << endl;
  return 0;
}
