#include <bits/stdc++.h>
using namespace std;
bool vis[2001];
vector<vector<int>> adj;
int height[2001];
void dfs(int node) {
  if (vis[node]) return;
  vis[node] = 1;
  for (auto j : adj[node]) {
    if (!vis[j]) height[j] = height[node] + 1;
    dfs(j);
  }
}
int main() {
  int i, j, m, n;
  cin >> n;
  adj.resize(n + 1);
  vector<int> check;
  for (i = 1; i <= n; i++) {
    int a;
    cin >> a;
    if (a != -1) {
      adj[a].push_back(i);
      adj[i].push_back(a);
    } else {
      check.push_back(i);
    }
  }
  for (auto i : check) {
    height[i] = 1;
    dfs(i);
  }
  int max = 0;
  for (i = 1; i <= n; i++) {
    if (height[i] > max) max = height[i];
  }
  cout << max;
}
