#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
const long double PI = acos(-1);
const int MOD = 998244353, LIM = 2e5 + 5;
const long double EPS = 1e-9;
const int mod = 1000000007;
int main() {
  int n;
  std::cin >> n;
  vector<int> v(n);
  for (int i = 0; i < v.size(); i++) {
    std::cin >> v[i];
  };
  int max_dp = 0;
  for (int i = 0; i < n; ++i) {
    int depth = 1;
    int next = v[i] - 1;
    while (next != -2) {
      depth++;
      next = v[next] - 1;
    }
    max_dp = max(max_dp, depth);
  }
  std::cout << max_dp << '\n';
}
