#include <bits/stdc++.h>
using namespace std;
int Adj_matrix[2009][2009];
int depth[2009];
int status[2009];
void dfs_visit(int v, int n) {
  status[v] = 1;
  int i;
  for (i = 1; i <= n; i++) {
    if (Adj_matrix[v][i] == 1) {
      if (status[i] == 0) dfs_visit(i, n);
      if (depth[i] + 1 > depth[v]) depth[v] = depth[i] + 1;
    }
  }
  status[v] = 2;
}
void dfs(int n) {
  int i;
  for (i = 1; i <= n; i++) {
    status[i] = 0;
    depth[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    if (status[i] == 0) dfs_visit(i, n);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int A[n], i;
  for (i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] == -1) continue;
    Adj_matrix[A[i]][i + 1] = 1;
  }
  dfs(n);
  int max = 0;
  for (i = 1; i <= n; i++) {
    if (depth[i] > max) max = depth[i];
  }
  cout << max + 1 << endl;
  return 0;
}
