#include <bits/stdc++.h>
using namespace std;
const int N = 10e4 + 5;
int u, v, c;
vector<int> adj[N];
vector<pair<int, int> > p;
bool vis[N];
void dfs(int u, int p) {
  c++;
  vis[u] = 1;
  for (int v : adj[u]) {
    if (v != p) {
      if (!vis[v]) {
        dfs(v, u);
      }
    }
  }
}
int par[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v;
    par[i] = v;
  }
  int d = -1;
  for (int i = 1; i <= n; i++) {
    int nc = 1;
    int x = i;
    while (par[x] != -1) {
      x = par[x];
      nc++;
    }
    d = max(d, nc);
  }
  cout << d << endl;
}
