#include <bits/stdc++.h>
using namespace std;
int a[2012], d[2012];
int dfs(int i) {
  if (d[i] != 0) return d[i];
  if (a[i] == -1) return 0;
  return d[i] = dfs(a[i]) + 1;
}
int main() {
  int n;
  cin >> n;
  int s = -100;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    s = max(s, dfs(i));
  }
  cout << s + 1;
}
