#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, j, n, nhap;
  cin >> n;
  vector<int> canh[n];
  vector<int> dinh;
  stack<int> lol;
  int rs[n];
  lol.push(0);
  for (i = 0; i < n; i++) {
    cin >> nhap;
    if (nhap != -1) {
      canh[nhap - 1].push_back(i);
    } else {
      rs[i] = 1;
      dinh.push_back(i);
    }
  }
  for (i = 0; i < dinh.size(); i++) {
    lol.push(dinh[i]);
    while (lol.size() > 0) {
      int dau = lol.top();
      lol.pop();
      for (j = 0; j < canh[dau].size(); j++) {
        lol.push(canh[dau][j]);
        rs[canh[dau][j]] = rs[dau] + 1;
      }
    }
  }
  cout << *max_element(rs, rs + n);
  return 0;
}
