#include <bits/stdc++.h>
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
  int res = 0;
  while (n && ++res) n -= n & (-n);
  return res;
}
template <class T>
void chmax(T &a, const T &b) {
  a = max(a, b);
}
template <class T>
void chmin(T &a, const T &b) {
  a = min(a, b);
}
long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) return 0;
  if (a == b) return a;
  if (a > b) return gcd(a - b, b);
  return gcd(a, b - a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
long long powermod(long long x, long long y) {
  long long res = 1;
  x = x % 1000000007;
  while (y > 0) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res;
}
long long mulmod(long long a, long long b) {
  long long res = 0;
  a %= 1000000007;
  while (b) {
    if (b & 1) res = (res + a) % 1000000007;
    a = (2 * a) % 1000000007;
    b >>= 1;
  }
  return res;
}
bool isPrime(long long 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 += 6) {
    if ((n % i == 0) || (n % (i + 2) == 0)) return false;
  }
  return true;
}
long double dist(long long x1, long long y1, long long x2, long long y2) {
  return (long double)sqrt(
      (long double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
}
long long squaredist(long long x1, long long y1, long long x2, long long y2) {
  return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
long long nCr(long long n, long long r) {
  if (r == 0) return 1;
  return (n * nCr(n - 1, r - 1)) / r;
}
long long countDivisors(long long n) {
  long long cnt = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i)
        cnt++;
      else
        cnt = cnt + 2;
    }
  }
  return cnt;
}
long long modulo(long long a, long long b) {
  long long r = a % b;
  return r < 0 ? r + b : r;
}
using namespace std;
class Graph {
  long long n;
  vector<bool> visited;
  vector<long long> dis, parent;

 public:
  vector<vector<long long>> adj;
  Graph(long long n);
  void addEdge(long long v, long long w);
  long long dfs();
  long long dfsVisit(long long v, long long count);
};
Graph::Graph(long long n) {
  this->n = n;
  adj.resize(n);
  visited.resize(n, false);
  dis.resize(n);
  parent.resize(n);
}
void Graph::addEdge(long long v, long long w) { adj[v].push_back(w); }
long long Graph::dfs() {
  long long cnt = INT_MIN, count = 0;
  for (long long i = 0; i < n; i++) {
    count = 0;
    if (!visited[i]) {
      parent[i] = -1;
      count = dfsVisit(i, count);
    }
    cnt = max(count, cnt);
  }
  return cnt;
}
long long Graph::dfsVisit(long long v, long long count) {
  ++count;
  visited[v] = true;
  for (long long u : adj[v]) {
    {
      parent[u] = v;
      count = max(count, dfsVisit(u, count));
    }
  }
  return count;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  Graph g1(n);
  for (long long i = 0; i < (n); i++) {
    long long z;
    cin >> z;
    if (z != -1) g1.addEdge(i, z - 1);
  }
  cout << max(1, g1.dfs());
  return 0;
}
