#include <bits/stdc++.h>
using namespace std;
vector<int> e[200000 + 4];
bool vis[200004] = {false};
int level[200005] = {0}, ans = 0;
int n;
void bfs(int a) {
  if (vis[a]) return;
  queue<int> q;
  q.push(a);
  vis[a] = true;
  int sz = 1;
  for (; !q.empty(); sz = q.size()) {
    while (sz--) {
      int v = q.front();
      q.pop();
      vis[v] = true;
      for (int i = 0; i < e[v].size(); i++) {
        if (!vis[e[v][i]]) {
          vis[e[v][i]] = true;
          q.push(e[v][i]);
        }
      }
    }
    ans++;
  }
}
vector<int> ro;
int main() {
  int x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x != -1) {
      e[x].push_back(i);
    } else
      ro.push_back(i);
  }
  int ma = -1;
  for (int i = 0; i < ro.size(); i++) {
    ans = 0;
    bfs(ro[i]);
    ma = max(ma, ans);
  }
  cout << ma << endl;
  return 0;
}
