#include <bits/stdc++.h>
using namespace std;
int arr[100000];
int p(int ind, int c) {
  if (arr[ind] == -1) {
    return c;
  } else {
    return p(arr[ind], c + 1);
  }
}
class Graph {
 private:
  int nodes;
  list<int> *adj;

 public:
  Graph(int nodes) {
    this->nodes = nodes;
    adj = new list<int>[nodes];
  }
  void addEdge(int x, int y) {
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  bool isCyclic(int nodes, bool visited[], int parent) {
    visited[nodes] = true;
    list<int>::iterator i;
    for (i = adj[nodes].begin(); i != adj[nodes].end(); i++) {
      if (!visited[*i]) {
        if (isCyclic(*i, visited, nodes)) {
          return true;
        }
      } else if (*i != parent) {
        return true;
      }
    }
    return false;
  }
  bool isTree() {
    bool *visited = new bool[nodes];
    for (int i = 0; i < nodes; i++) {
      visited[i] = false;
    }
    if (isCyclic(0, visited, -1)) {
      return false;
    }
    for (int u = 0; u < nodes; u++) {
      if (!visited[u]) {
        return false;
      }
    }
    return true;
  }
};
int main() {
  int n;
  cin >> n;
  int maxi = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= n; i++) {
    maxi = max(maxi, p(i, 1));
  }
  cout << maxi;
}
