#include <bits/stdc++.h>
using namespace std;
vector<int> g[101010];
bool u[1010];
void dfs(int x) {
  u[x] = true;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (!u[to]) {
      u[to] = true;
      dfs(to);
    }
  }
}
long long n, s = 0, c = 0;
int a[101010];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] != -1) {
      g[a[i]].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j != -1) {
      j = a[j];
      s++;
    }
    c = max(c, s);
    s = 0;
  }
  cout << c;
}
