#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
long long n, e;
int visited[100001];
int m[100001];
vector<int> count2;
bool flag = true;
int dfs(int sv) {
  if (m[sv] != -1 && flag) {
    return 1;
  } else
    flag = false;
  if (visited[sv]) return 0;
  visited[sv] = 1;
  int ans = 0;
  for (long long x : v[sv]) {
    int ans1 = dfs(x);
    ans = max(ans1, ans);
  }
  return 1 + ans;
}
int main() {
  cin >> n;
  vector<int> take;
  for (long long i = 1; i < n + 1; i++) {
    int data;
    cin >> data;
    m[i] = data;
    if (data == -1) continue;
    take.push_back(data);
    v[data].push_back(i);
  }
  int sol = 1;
  for (int i = 0; i < n + 1; i++) {
    if (!visited[i]) {
      int ask = dfs(i);
      if (ask == 1) visited[i] = 0;
      sol = max(ask, sol);
      flag = true;
    }
  }
  cout << sol;
}
