#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int parents[n];
  vector<vector<int> > graph(n);
  for (int i = 0; i < n; i++) {
    cin >> parents[i];
    if (parents[i] != -1) {
      graph[parents[i] - 1].push_back(i);
    }
  }
  queue<pair<int, int> > que;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (parents[i] == -1) {
      que.push({i, 1});
    }
  }
  while (que.size()) {
    int idx = que.front().first;
    int cnt = que.front().second;
    mx = max(cnt, mx);
    que.pop();
    for (int i = 0; i < graph[idx].size(); i++) {
      que.push({graph[idx][i], cnt + 1});
    }
  }
  cout << mx << endl;
  return 0;
}
