#include <bits/stdc++.h>
using namespace std;
map<int, int> mym;
int rec(int i) {
  if (mym[i] == -1) return 1;
  return rec(mym[i]) + 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int manager;
    cin >> manager;
    mym[i] = manager;
  }
  int maxi = -1;
  for (map<int, int>::iterator it = mym.begin(); it != mym.end(); it++)
    maxi = max(maxi, rec(it->first));
  cout << maxi;
  return 0;
}
