using namespace std;
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdio>
#include <set>
#include <cctype>
#include <map>
#include <cmath>
#include <queue>
#include <algorithm>
#include <stack>
#include <cctype>
#include <cstring>
#include <string>
#define MAX 10100
#define PRIME 31
#define MOD 1000000007
#define PI 3.1415926535897932384
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
typedef long long ll;
vector<int> adj[MAX];
int maxi, dia_end, n;
void dfs(int ind, int prev, int cont){
if(cont > maxi){
maxi = cont;
dia_end = ind; // Possible end of tree's diameter
}
for(int i = 0;i < adj[ind].size();i++){
if(adj[ind][i] == prev) continue;
dfs(adj[ind][i], ind, cont+1);
}
}
int main(){
ios::sync_with_stdio(false);
//freopen("in.txt", "r", stdin);
while(cin >> n && n != -1){
// Initialize
for(int i = 1;i <= n;i++) adj[i] = vector<int>();
maxi = 0;
for(int x, i = 2;i <= n;i++){
cin >> x;
adj[x].pb(i);
adj[i].pb(x);
}
// Find one end of the tree's diameter
dfs(1, -1, 1);
// Find the total length of the tree's diameter
dfs(dia_end, -1, 1);
// Time needed is total length/2
cout << maxi/2 << endl;
}
return 0;
}
dXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGNjdHlwZT4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGNjdHlwZT4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxzdHJpbmc+CgojZGVmaW5lIE1BWCAxMDEwMAojZGVmaW5lIFBSSU1FIDMxCiNkZWZpbmUgTU9EIDEwMDAwMDAwM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