#include <bits/stdc++.h>
using namespace std;
vector<int> v[10005];
bool visited[10005] = {false};
int mx = 0;
void dfs(int src, int depth) {
  int node = src;
  visited[node] = true;
  for (int i : v[node]) {
    if (!visited[i]) {
      depth++;
      mx = max(mx, depth);
      dfs(i, depth);
    }
    depth--;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int grp = 0;
  int x;
  int depth = 0;
  int root = -1;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == -1) {
      continue;
    }
    x--;
    v[x].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i, 0);
      grp = max(mx, grp);
      mx = 0;
      for (int i = 0; i < n; i++) visited[i] = false;
    }
  }
  cout << grp + 1 << '\n';
  return 0;
}
