#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    int x = 0;
    int p = i;
    while (p != -2) {
      p = a[p];
      x++;
    }
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}
