#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int> >& graph, int s, vector<bool>& visited,
         vector<int>& level) {
  visited[s] = true;
  for (auto child : graph[s]) {
    if (!visited[child]) {
      visited[child] = true;
      level[child] = level[s] + 1;
      dfs(graph, child, visited, level);
    }
  }
}
void solve() {
  int n;
  cin >> n;
  vector<vector<int> > graph(n);
  vector<int> start;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (x == -1) {
      start.push_back(i);
    } else {
      graph[x - 1].push_back(i);
    }
  }
  vector<bool> visited(n, false);
  vector<int> level(n, 1);
  for (int i = 0; i < start.size(); ++i) {
    dfs(graph, start[i], visited, level);
  }
  int res = 0;
  for (int i = 0; i < n; ++i) res = max(res, level[i]);
  cout << res << "\n";
}
void pre() {
  int t = 1;
  while (t--) {
    solve();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  pre();
  return 0;
}
