#include <bits/stdc++.h>
using namespace std;
int n, t;
int dinh[2001];
int kq[2001], dem = 0;
vector<int> canh[2001];
int d = 0;
bool f[2001];
int cnt;
void DFS(int u) {
  f[u] = false;
  cnt++;
  for (int i = 0; i < canh[u].size(); i++)
    if (f[canh[u][i]] == true) DFS(canh[u][i]);
  kq[d] = max(kq[d], cnt);
  cnt--;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t;
    if (t == -1) {
      dem++;
      dinh[dem] = i;
    } else
      canh[t].push_back(i);
  }
  fill(f + 1, f + n + 1, true);
  for (int i = 1; i <= dem; i++) {
    cnt = 0;
    d++;
    DFS(dinh[i]);
  }
  int smin = -20000;
  for (int i = 1; i <= d; i++)
    if (kq[i] > smin) smin = kq[i];
  cout << smin << "\n";
}
