#include <bits/stdc++.h>
using namespace std;
int h[200000];
int visited[200000];
vector<int> tree[1000005];
void dfs(int v, int pv) {
  visited[v] = 1;
  h[v] = h[pv] + 1;
  for (auto u : tree[v])
    if (u != pv) dfs(u, v);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    if (p != -1) {
      tree[p].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++)
    if (not visited[i]) dfs(i, 0);
  cout << *max_element(h + 1, h + 1 + n);
  return 0;
}
