#include <bits/stdc++.h>
using namespace std;
vector<long long> a[(long long)2e5 + 1];
bool ch[(long long)2e5 + 1] = {false};
long long s;
bool f = true;
long long level[(long long)2e5 + 1];
void bfs(long long i) {
  queue<long long> q;
  q.push(i);
  level[i] = 1;
  ch[i] = true;
  while (!q.empty()) {
    long long o = q.front();
    q.pop();
    for (long long j = 0; j < a[o].size(); j++) {
      if (ch[a[o][j]] == false) {
        ch[a[o][j]] = true;
        level[a[o][j]] = level[o] + 1;
        q.push(a[o][j]);
      }
    }
  }
}
int main() {
  long long i, j, n, k, l, m;
  cin >> n;
  vector<long long> d;
  for (i = 1; i <= n; i++) {
    cin >> l;
    if (l == -1) {
      d.push_back(i);
      continue;
    }
    a[l].push_back(i);
  }
  long long ma = 0;
  for (i = 0; i < d.size(); i++) {
    for (j = 1; j <= n; j++) {
      ch[j] = false;
    }
    bfs(d[i]);
    for (j = 1; j <= n; j++) {
      ma = max(ma, level[j]);
    }
  }
  cout << ma;
}
