#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
vector<int> depth;
int findDepth(int i) {
  int dep = 0;
  while (i != -1) {
    i = arr[i];
    dep++;
  }
  return dep;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  arr.resize(n + 1);
  depth.resize(n + 1);
  for (int i = 1; i <= n; i++) cin >> arr[i];
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    mx = max(mx, findDepth(i));
  }
  cout << mx;
  return 0;
}
