#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int n, d[N], ans = -1;
vector<int> v[N];
vector<int> root;
void dfs(int s, int p) {
  d[s] = d[p] + 1;
  ans = max(ans, d[s]);
  for (int i = 0; i < v[s].size(); i++)
    if (v[s][i] != p) dfs(v[s][i], s);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int x;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x != -1) {
      v[i].push_back(x);
      v[x].push_back(i);
    } else {
      root.push_back(i);
    }
  }
  for (int i = 0; i < root.size(); i++) {
    dfs(root[i], 0);
  }
  cout << ans;
  return 0;
}
