#include <bits/stdc++.h>
using namespace std;
int n;
list<int> adj_list[2001];
bool vis[2001];
int level[2001];
int dfs(int x) {
  queue<int> q;
  q.push(x);
  level[x] = 1;
  vis[x] = true;
  int ans = 1;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (auto it = adj_list[p].begin(); it != adj_list[p].end(); it++) {
      if (vis[*it] == false) {
        level[*it] = level[p] + 1;
        ans = max(ans, level[*it]);
        q.push(*it);
        vis[*it] = true;
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= 2000; i++) vis[i] = false;
  vector<int> visitors;
  for (int i = 1; i <= n; i++) {
    int temp1;
    cin >> temp1;
    if (temp1 != -1) {
      adj_list[temp1].push_back(i);
      adj_list[i].push_back(temp1);
    } else
      visitors.push_back(i);
  }
  int max_depth = 0;
  for (int i = 0; i < visitors.size(); i++)
    max_depth = max(max_depth, dfs(visitors[i]));
  cout << max_depth;
}
