#include <bits/stdc++.h>
using namespace std;
vector<int> v[2010];
int par[2010];
bool visit[2010];
int main() {
  int i, j, n, lastr, t, x;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x;
    if (x == -1) {
      par[i] = x;
      continue;
    }
    v[x].push_back(i);
    par[i] = x;
  }
  t = 1;
  int sum = 0;
  for (i = 1; i <= n; i++) {
    if (par[i] == -1) {
      queue<pair<int, int> > q;
      q.push(make_pair(i, t));
      lastr = 1;
      while (!q.empty()) {
        pair<int, int> pp;
        pp = q.front();
        for (j = 0; j < v[pp.first].size(); j++) {
          q.push(make_pair(v[pp.first][j], 1 + pp.second));
          lastr = pp.second + 1;
        }
        q.pop();
      }
      sum = max(sum, lastr);
    }
  }
  cout << sum << endl;
  return 0;
}
