#include <bits/stdc++.h>
using namespace std;
int bfs(std::vector<list<int>> &adjacency_list, int n, int s) {
  vector<int> dist(n, -1);
  std::vector<bool> visited(n, false);
  dist[s] = 0;
  visited[s] = true;
  int ans = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (auto it = adjacency_list[node].begin();
         it != adjacency_list[node].end(); it++) {
      if (!visited[*it]) {
        dist[*it] = dist[node] + 1;
        visited[*it] = true;
        ans = max(ans, dist[*it]);
        q.push(*it);
      }
    }
  }
  return ans;
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, a;
  cin >> n;
  vector<int> boss;
  std::vector<list<int>> adjacency_list(n);
  for (int i = 0; i <= n - 1; i++) {
    cin >> a;
    if (a == -1)
      boss.push_back(i);
    else
      (adjacency_list[a - 1]).push_back(i);
  }
  int ans = INT_MIN;
  for (auto it = boss.begin(); it != boss.end(); it++) {
    ans = max(ans, bfs(adjacency_list, n, *it) + 1);
  }
  cout << ans << endl;
  return 0;
}
