#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
int n;
vector<vector<int>> g;
vector<int> start;
int dfs(int ind) {
  int res = 0;
  for (auto& i : g[ind]) {
    res = max(dfs(i), res);
  }
  return res + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int j;
  cin >> n;
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> j;
    if (j < 0)
      start.push_back(i);
    else {
      g[--j].push_back(i);
    }
  }
  int maxi = 0;
  for (auto& i : start) maxi = max(dfs(i), maxi);
  cout << maxi << endl;
  return 0;
}
