#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cur;
  int ans = 0, count;
  int parent[2100];
  int lvl[2100];
  queue<int> fila;
  vector<int> adlist[2100];
  for (int i = 0; i <= 2000; i++) lvl[i] = 0;
  lvl[2010] = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> parent[i];
    if (parent[i] != -1)
      adlist[parent[i]].push_back(i);
    else
      parent[i] = 2010;
  }
  for (int i = 1; i <= n; i++) {
    if (parent[i] == 2010) {
      fila.push(i);
      while (!fila.empty()) {
        cur = fila.front();
        fila.pop();
        lvl[cur] = lvl[parent[cur]] + 1;
        ans = max(ans, lvl[cur]);
        if (!adlist[cur].empty()) {
          int sz = adlist[cur].size();
          for (int i = 0; i < sz; i++) fila.push(adlist[cur][i]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
