#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 30);
const int N = 2005;
const int mod = 1007;
const double eps = 1e-10;
int n, m;
vector<int> e[N];
bool in[N];
int dfs(int u, int f, int dep) {
  int mx = dep;
  for (int i = 0; i < e[u].size(); i++) {
    int v = e[u][i];
    if (v == f) continue;
    mx = max(mx, dfs(v, u, dep + 1));
  }
  return mx;
}
int main() {
  int i, j, k, t, cas = 0;
  int a, b;
  while (scanf("%d", &n) != EOF) {
    for (i = 0; i <= n; i++) e[i].clear();
    memset(in, 0, sizeof(in));
    for (i = 1; i <= n; i++) {
      scanf("%d", &a);
      if (a == -1) continue;
      e[a].push_back(i);
      in[i] = 1;
    }
    int ret = 0;
    for (i = 1; i <= n; i++)
      if (!in[i]) ret = max(ret, dfs(i, i, 1));
    printf("%d\n", ret);
  }
  return 0;
}
