#include <bits/stdc++.h>
using namespace std;
int maxm = 1;
void bfs(vector<int> *g, pair<int, int> *visited, int s) {
  queue<pair<int, int>> q;
  visited[s].first = 1;
  q.push({s, 1});
  while (!q.empty()) {
    pair<int, int> front = q.front();
    q.pop();
    int f = front.first;
    int s = front.second;
    for (auto i : g[f]) {
      if (visited[i].first == false) {
        visited[i].first = 1;
        q.push({i, s + 1});
        if ((s + 1) > maxm) {
          maxm = s + 1;
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  pair<int, int> visited[n + 1];
  for (int i = 1; i <= n; i++) {
    visited[i] = {0, -1};
  }
  vector<int> g[n + 1];
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (num == -1) {
      visited[i + 1].second = INT_MIN;
      continue;
    }
    g[i + 1].push_back(num);
    g[num].push_back(i + 1);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i].first == 0 && visited[i].second == INT_MIN) {
      bfs(g, visited, i);
    }
  }
  cout << maxm;
}
