#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define make0(a) memset(a,0,sizeof(a))
#define make1(a) memset(a,-1,sizeof(a))
#define all(v) v.begin(),v.end()
#define sc(i) scanf("%d",&i)
#define pi pair <int,int>
const int mod = 1e9+7;
const int N = 1e5+1;
int max(int a,int b,int c) { return max(max(a,b),c); }
int max(int a[],int n) { int mx=a[0]; for(int i=0;i<n;i++) {if(mx<a[i]) { mx=a[i]; } } return mx;}
int max(vector<int> a) { int n=a.size(); int mx=a[0]; for(int i=0;i<n;i++) {if(mx<a[i]) { mx=a[i]; } } return mx;}
int min(int a,int b,int c) { return min(min(a,b),c); }
int min(int a[],int n) { int mn=a[0]; for(int i=0;i<n;i++) {if(mn>a[i]) { mn=a[i]; } } return mn;}
int min(vector<int> a) { int n=a.size(); int mn=a[0]; for(int i=0;i<n;i++) {if(mn>a[i]) { mn=a[i]; } } return mn;}
void input(int a[],int n) { for (int i = 0; i < n; ++i) { scanf("%d",&a[i]); } }
void input(vector <int> &a,int n) { for (int i = 0; i < n; ++i) { scanf("%d",&a[i]); } }
bool isPalin(string str) { int n=str.size(); for(int i=0;i<n/2;i++) { if(str[i] != str[n-1-i]) return false;} return true; }
vector <int> adjlist[N];
int f[N],g[N];
int dia = 0;
void dfs(int i)
{
vector <int> fv;
for(int j : adjlist[i])
{
dfs(j);
fv.pb(f[j]);
}
sort(all(fv));
f[i] = 0;
if(fv.size() > 0)
{
f[i] = 1 + fv.back();
}
if(fv.size() >= 2)
{
g[i] = 2 + fv.back() + fv[fv.size()-2];
}
// for(int x : fv)
// cout << x << ' ';
// cout << endl;
dia = max(dia,g[i],f[i]);
}
int main()
{
int n;
cin >> n; // number of nodes
for(int i=2;i<=n;i++)
{
int a;
cin >> a; // parent of the ith node.
adjlist[a].pb(i);
}
dfs(1);
cout << dia << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2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