#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<bool> visited;
vector<int> level;
int z = 0;
void bfs(int s) {
  queue<int> queue;
  queue.push(s);
  if (level[s] == 0) level[s] = 1;
  while (!queue.empty()) {
    s = queue.front();
    queue.pop();
    for (int i = 0; i < adj[s].size(); i++) {
      int child = adj[s][i];
      level[child] = level[s] + 1;
      queue.push(child);
    }
  }
}
void dfs(int v, int c) {
  z = max(z, c);
  if (visited[v]) return;
  visited[v] = true;
  for (int i = 0; i < adj[v].size(); i++) {
    int child = adj[v][i];
    if (!visited[child]) {
      dfs(child, c + 1);
    }
  }
}
int main() {
  int n, z;
  cin >> n;
  adj = vector<vector<int> >(n + 1);
  level = vector<int>(n + 1);
  visited = vector<bool>(n + 1, false);
  vector<int> qrr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> z;
    if (z != -1) {
      adj[z].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    bfs(i);
  }
  int m = *max_element(level.begin(), level.end());
  cout << m << endl;
  return 0;
}
