#include <bits/stdc++.h>
const int N = 2010;
const int inf = 0x3f3f3f3f;
using namespace std;
int n;
vector<int> vt[N];
int dp[N], p[N];
bool vis[N];
void dfs(int u, int p) {
  vis[u] = 1;
  dp[u] = 0;
  int sz = vt[u].size(), ret = 0;
  for (int i = 0; i < sz; i++) {
    int v = vt[u][i];
    if (v == p) continue;
    dfs(v, u);
    ret = max(ret, dp[v]);
  }
  dp[u] = ret + 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i] == -1) continue;
    vt[p[i]].push_back(i);
    vt[i].push_back(p[i]);
  }
  int ret = 0;
  for (int i = 1; i <= n; i++)
    if (p[i] == -1) dfs(i, i), ret = max(ret, dp[i]);
  printf("%d\n", ret);
  return 0;
}
