#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 11;
int n, x, t[N], maxx;
bool used[N];
vector<int> g[N];
void dfs(int v, int glu) {
  used[v] = true;
  t[v] = glu;
  maxx = max(maxx, glu);
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) {
      dfs(to, glu + 1);
    } else if (used[to] && t[to] <= glu) {
      dfs(to, glu + 1);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    if (x != -1) {
      g[x].push_back(i);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!used[i]) {
      dfs(i, 1);
    }
  }
  cout << maxx;
  return 0;
}
