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