#include <bits/stdc++.h>   using namespace std;   int v[300123], n, ans, g[300123], f[300123], a, b, m, op;   queue<int> q;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> v[i];       if (~v[i])         g[v[i]]++;       else {         g[0]++;       }     }     ans = 1;     for (int i = 1; i <= n; ++i) {       if (g[i] == 0) {         q.push(i);         f[i] = 1;       }     }     while (!q.empty()) {       int u = q.front();       q.pop();       g[v[u]]--;       if (g[v[u]] == 0) {         f[v[u]] = f[u] + 1;         q.push(v[u]);         ans = max(ans, f[v[u]]);       }     }     cout << ans << endl;     return 0;   }   