#include <bits/stdc++.h>
using namespace std;
long long int BIGMOD(long long int a, long long int p, long long int m) {
  long long int result = 1;
  long long int x = a;
  while (p) {
    if (p & 1) {
      result = (result * x) % m;
    }
    x = (x * x) % m;
    p = p >> 1;
  }
  return result;
}
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
void SieveOfEratosthenes(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
int maxx_level = -1;
int visited[2005];
void initialize(int n) {
  for (int i = 0; i < n + 5; i++) visited[i] = 0;
}
vector<int> vc[2005];
stack<int> s;
void dfs(int src, int level) {
  s.push(src);
  visited[src] = 1;
  maxx_level = max(maxx_level, level);
  for (int i = 0; i < vc[src].size(); i++) {
    if (!visited[vc[src][i]]) dfs(vc[src][i], level + 1);
  }
}
int main() {
  int n;
  cin >> n;
  int x;
  for (int i = 1; i < n + 1; i++) {
    cin >> x;
    if (x == -1)
      continue;
    else {
      vc[x].push_back(i);
    }
  }
  int lev = 0;
  for (int i = 1; i < n + 1; i++) {
    initialize(n);
    dfs(i, 1);
  }
  cout << maxx_level << "\n";
  return 0;
}
