#include <bits/stdc++.h>
using namespace std;
map<int, bool> visited;
int bfs(int s, int n, vector<int> adj[]) {
  queue<int> visit;
  visit.push(s);
  visited[s] = true;
  int ht = 1, h = 1;
  map<int, int> lvl;
  lvl[s] = 1;
  while (!visit.empty()) {
    int v = visit.front();
    visit.pop();
    if (!adj[v].size()) continue;
    for (int u : adj[v]) {
      if (!visited[u]) {
        visited[u] = true;
        visit.push(u);
        lvl[u] = lvl[v] + 1;
        h = max(h, lvl[u]);
      }
    }
  }
  return h;
}
int main() {
  int n, x;
  cin >> n;
  vector<int> adj[n + 1], tr;
  visited.clear();
  for (int i = 1; i != n + 1; i += 1) {
    cin >> x;
    if (x == -1) {
      tr.push_back(i);
      continue;
    }
    adj[x].push_back(i);
    adj[i].push_back(x);
  }
  int height = 1;
  for (int au : tr) {
    visited.clear();
    height = max(height, bfs(au, n, adj));
  }
  cout << height << endl;
  return 0;
}
