#include <bits/stdc++.h>
using tree = std::vector<std::list<int>>;
int height(const tree& organization, int manager = 0) {
  int levels = 0;
  for (const int& employee : organization[manager]) {
    levels = std::max(levels, height(organization, employee));
  }
  return levels + 1;
}
int main(int argc, char const* argv[]) {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int n, parent, k;
  std::cin >> n;
  tree organization(n + 1);
  for (int k = 1; k <= n; k++) {
    std::cin >> parent;
    organization[parent == -1 ? 0 : parent].push_back(k);
  }
  std::cout << height(organization) - 1 << std::endl;
  return 0;
}
