#include <bits/stdc++.h>
using namespace std;
long long int n = 0;
vector<vector<long long int> > adj;
vector<bool> visited(10000);
long long int depth[10000];
queue<long long int> q;
void dfs(long long int v) {
  visited[v] = true;
  for (long long int u = 0; u < adj[v].size(); u++) {
    long long int k = adj[v][u];
    if (visited[k] == false) {
      depth[k] = depth[v] + 1;
      dfs(k);
    }
  }
  return;
}
int main() {
  long long int x = 0, j = 0;
  cin >> n;
  adj.resize(n + 1);
  vector<long long int> v(n + 1);
  for (long long int i = 1; i < n + 1; i++) {
    cin >> v[i];
    x = v[i];
    if (x > 0) {
      adj[x].push_back(i);
      adj[i].push_back(x);
    }
  }
  for (long long int i = 1; i < n + 1; i++) depth[i] = 0;
  for (long long int i = 1; i < n + 1; i++) {
    if (v[i] == -1) {
      depth[i] = 1;
      dfs(i);
    }
  }
  for (long long int i = 1; i < n + 1; i++) j = max(j, depth[i]);
  cout << j << endl;
  return 0;
}
