#include <bits/stdc++.h>
using namespace std;
vector<long long> v[2005];
long long vis[2005];
long long dis[2005];
void dfs(long long node) {
  vis[node] = 1;
  for (long long i = 0; i < (long long)((v[node]).size()); i++) {
    if (vis[v[node][i]] == 0) {
      if (dis[v[node][i]] == 0) {
        dfs(v[node][i]);
        dis[node] = dis[v[node][i]] + 1;
      } else {
        dis[node] = dis[v[node][i]] + 1;
      }
    } else {
      dis[node] = dis[v[node][i]] + 1;
    }
  }
}
void solve() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long t;
    cin >> t;
    if (t == -1) {
      dis[i] = 1;
    } else {
      v[i].push_back(t);
    }
  }
  for (long long i = 1; i <= n; i++) {
    dfs(i);
  }
  long long mx = -1;
  for (long long i = 1; i <= n; i++) {
    mx = max(mx, dis[i]);
  }
  cout << mx << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
