#include <bits/stdc++.h>
using namespace std;
bool visited[2000];
vector<int> G[2000];
vector<int> parent;
int dfs(int u) {
  if (visited[u]) {
    return 0;
  }
  visited[u] = true;
  int ans = 0;
  for (int i = 0; i < G[u].size(); i++) {
    ans = max(ans, dfs(G[u][i]));
  }
  return ans + 1;
}
int ans(int n) {
  int a = 0;
  for (int i = 0; i < parent.size(); i++) {
    a = max(a, dfs(parent[i]));
  }
  return a;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    int u;
    scanf("%d", &u);
    if (u != -1) {
      u--;
      G[u].push_back(i);
    } else {
      parent.push_back(i);
    }
  }
  printf("%d\n", ans(n));
}
