#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
long long bigmod(long long b, long long p, long long m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    long long temp = bigmod(b, p / 2, m);
    return (temp * temp) % m;
  }
  return (bigmod(b, p - 1, m) * (b % m)) % m;
}
vector<vector<int> > v;
vector<bool> visited;
int n, e;
int dfs(int node) {
  if (v[node].size() == 0) return 1;
  for (int i = 0; i < v[node].size(); i++) return dfs(v[node][i]) + 1;
}
int connectedcomponenetscnt() {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  }
}
int main() {
  int x, maxx = 0;
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> x;
    if (x != -1) v[i].push_back(x);
  }
  for (int i = 1; i <= n; i++) maxx = max(maxx, dfs(i));
  cout << maxx;
}
