#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ";
  err(++it, args...);
}
const int MAX = 1E6 + 10;
int visited[MAX], parent[MAX];
vector<vector<int> > adj(MAX);
int DFS(int u, int p) {
  int res = 0;
  visited[u] = 1;
  for (int v : adj[u]) {
    if (v != p) {
      res = max(res, DFS(v, u));
    }
  }
  return res + 1;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    if (p != -1) {
      parent[i] = p;
      adj[p].push_back(i);
    }
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i] && parent[i] == 0) res = max(res, DFS(i, 0));
  }
  cout << res;
}
