#include <bits/stdc++.h>
using namespace std;
bool visited[2002];
int d[2002];
vector<int> graph[2002];
int bfs(int start) {
  queue<int> Q;
  Q.push(start);
  memset(visited, false, sizeof(visited));
  memset(d, 0, sizeof(d));
  d[start] = 1;
  visited[start] = true;
  int mx = d[start];
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (unsigned int j = 0; j < graph[u].size(); j++) {
      if (!visited[graph[u][j]]) {
        visited[graph[u][j]] = true;
        d[graph[u][j]] = d[u] + 1;
        mx = max(mx, d[graph[u][j]]);
        Q.push(graph[u][j]);
      }
    }
  }
  return mx;
}
int main() {
  int n, a[2002];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    if (a[i + 1] != -1) graph[a[i + 1]].push_back(i + 1);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == -1) {
      int x = bfs(i);
      ans = max(ans, x);
    }
  }
  cout << ans << endl;
  return 0;
}
