#include <bits/stdc++.h>
using namespace std;
int T;
int N, M;
string S;
vector<int> data;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  vector<int> adjList[2005];
  queue<int> q;
  bool visit[2005] = {false};
  int dist[2005];
  cin >> N;
  for (int i = int(1); i <= int(N); i++) {
    int v;
    cin >> v;
    if (v == -1) {
      q.push(i);
      visit[i] = true;
      dist[i] = 1;
    } else {
      adjList[v].push_back(i);
    }
  }
  int groupCnt = 1;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    vector<int>& neighbors = adjList[cur];
    for (int i = int(0); i <= int(neighbors.size() - 1); i++) {
      int nxt = neighbors[i];
      if (visit[nxt]) continue;
      visit[nxt] = true;
      dist[nxt] = dist[cur] + 1;
      q.push(nxt);
      groupCnt = max(groupCnt, dist[nxt]);
    }
  }
  cout << groupCnt << endl;
  return 0;
}
