#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  ++n;
  vector<int> e;
  e.push_back(0);
  copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(e));
  int g = 1;
  for (int(i) = int(1); (i) < int(n); (i)++) {
    int m = i;
    int lg = 0;
    for (;;) {
      m = e[m];
      ++lg;
      if (m == -1) break;
    }
    g = max(g, lg);
  }
  cout << g;
  return 0;
}
