#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
vector<int> g[N];
bool seen[N];
int n;
void read() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    if (a != -1) {
      g[a].push_back(i);
    }
  }
}
int res = 0;
void dfs(int v, int d) {
  res = max(res, d);
  for (int i : g[v]) {
    dfs(i, d + 1);
  }
}
int main() {
  read();
  for (int i = 1; i <= n; ++i) {
    dfs(i, 1);
  }
  cout << res << endl;
}
