#include <bits/stdc++.h>
using namespace std;
vector<int> v[10000];
bool vis[10000];
int dfs(int n) {
  vis[n] = true;
  int m = 1;
  for (int i = 0; i < v[n].size(); i++) {
    if (vis[v[n][i]] == false) {
      m = max(m, dfs(v[n][i]) + 1);
    }
  }
  return m;
}
int main() {
  int n, x, m;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x != -1) {
      v[x].push_back(i);
    } else {
      v[0].push_back(i);
    }
  }
  m = 0;
  for (int i = 0; i < v[0].size(); i++) {
    if (vis[v[0][i]] == false) {
      m = max(m, dfs(v[0][i]));
    }
  }
  cout << m << endl;
  return 0;
}
