#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 100;
const long long LINF = 1e18 + 228;
const double DINF = 5e18 + .0;
const int ITER = 300;
const double EPS = 1e-6;
const int MOD = 1000000007;
const long long MAXN = 5e3 + 10;
vector<vector<int>> g;
bool used[MAXN];
bool par[MAXN];
int ans = 0;
void dfs(int v, int lvl = 1) {
  used[v] = true;
  ans = max(ans, lvl);
  for (int i = 0; i < (int)g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) dfs(to, lvl + 1);
  }
}
int main() {
  int n;
  cin >> n;
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x == -1)
      par[i] = true;
    else
      g[x - 1].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    if (par[i]) dfs(i);
  }
  cout << ans;
  return 0;
}
