#include <bits/stdc++.h>
using namespace std;
long long INF = 1E9;
long long M = 1E9 + 7;
inline long long max_(long long a, long long b) { return (a > b) ? a : b; }
inline long long min_(long long a, long long b) { return (a < b) ? a : b; }
inline long long power(long long a, long long p) {
  long long res = 1, x = a;
  while (p) {
    if (p & 1) res = (res * x);
    x = (x * x);
    p >>= 1;
  }
  return res;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (T x : p) os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "{ ";
  for (T x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const map<Tk, Tv>& p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
long long N;
vector<long long> adj[2005];
vector<long long> start;
long long ans = 0;
void dfs(long long x, long long len) {
  if (adj[x].size() == 0) ans = max(ans, len);
  long long i;
  for (i = 0; i < adj[x].size(); i++) {
    dfs(adj[x][i], len + 1);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("inp.txt", "r")) {
    freopen("inp.txt", "r", stdin);
    freopen("outp.txt", "w", stdout);
  }
  cin >> N;
  long long i, p;
  for (i = 1; i <= N; i++) {
    cin >> p;
    if (p == -1) {
      start.push_back(i);
    } else {
      adj[p].push_back(i);
    }
  }
  for (i = 0; i < start.size(); i++) dfs(start[i], 1);
  cout << ans << endl;
  return 0;
}
