#include <bits/stdc++.h>
using namespace std;
int mx = 0;
vector<vector<int> > V;
vector<int> d;
int bfs(int n) {
  queue<int> q;
  q.push(n);
  d[0] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < (int)V[u].size(); i++) {
      int v = V[u][i];
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        mx = max(mx, d[v]);
        q.push(v);
      }
    }
  }
  return mx;
}
int main() {
  int n, x;
  cin >> n;
  V.assign(n + 1, vector<int>());
  d.assign(n + 1, -1);
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == -1)
      V[0].push_back(i + 1);
    else
      V[x].push_back(i + 1);
  }
  cout << bfs(0);
}
