#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> v[maxn];
int ma = 0;
int h = 0;
int f = 0;
int root = 0;
int g[maxn];
bool mark[maxn];
void dfs(int k) {
  if (mark[k] == false) {
    mark[k] = true;
  } else {
    return;
  }
  for (int i = 0; i < v[k].size(); i++) {
    int child = v[k][i];
    if (mark[child] == false) {
      g[h]++;
      dfs(child);
    }
  }
  if (v[k].size() == 1 && root != k) {
    if (g[h] + 1 > ma) {
      ma = g[h] + 1;
    }
    f = 1;
  }
  if (f == 1) {
    g[h]--;
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u;
    cin >> u;
    if (u == -1) {
      mark[i] = true;
    } else {
      v[i].push_back(u);
      v[u].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (mark[i] == true) {
      mark[i] = false;
      root = i;
      dfs(i);
      h++;
    }
  }
  if (ma == 0) {
    cout << 1;
    return 0;
  }
  cout << ma;
}
