#include <bits/stdc++.h>
using namespace std;
int n, a[200000], x, ans;
bool vis[100000] = {false};
vector<int> v[100000];
int dfs(int src) {
  vis[src] = true;
  int mx = 0;
  for (int i = 0; i < v[src].size(); i++) {
    mx = max(mx, dfs(v[src][i]));
  }
  return mx + 1;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x != -1) v[x].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}
