#include <bits/stdc++.h>
using namespace std;
void file() {}
const int MN = 5e4 + 1e2;
const int MW = 1e3 + 5;
const int OO = 1e9 + 5;
int N, x, vs[2005];
vector<int> adj[2005];
int bfs(int s, int cur = 0, int dep = 0, int siz = 0) {
  queue<int> q;
  vs[s] = 1;
  q.push(s);
  for (dep = 1, siz = q.size(); !q.empty(); ++dep, siz = q.size())
    while (siz--) {
      cur = q.front(), q.pop();
      for (auto it : adj[cur])
        if (!vs[it]) vs[it] = 1, q.push(it);
    }
  return dep - 1;
}
int main() {
  file();
  scanf("%d", &N);
  for (int i = 1; i <= N and scanf("%d", &x); ++i)
    adj[(x == -1) ? 0 : x].push_back(i);
  int ret = 0;
  for (auto it : adj[0]) memset(vs, 0, sizeof(vs)), ret = max(ret, bfs(it));
  printf("%d", ret);
  return 0;
}
