stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <vector>

#include <string>

#include <map>

#include <set>

#include <cassert>

using namespace std;

#define rep(i,a,n) for (int i=a;i<n;i++)

#define per(i,a,n) for (int i=n-1;i>=a;i--)

#define pb push_back

#define mp make_pair

#define all(x) (x).begin(),(x).end()

#define fi first

#define se second

#define SZ(x) ((int)(x).size())

typedef vector<int> VI;

typedef long long ll;

typedef pair<int,int> PII;

const ll mod=1000000007;

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

// head



const int N=101000;

int n,f[N],dp[N];

VI r[N],s[N];

void dfs(int u) {

	r[u].clear();

	for (auto v:s[u]) {

		dfs(v);

		r[u].pb(dp[v]);

	}

	sort(all(r[u]));

	reverse(all(r[u]));

	rep(i,0,SZ(r[u])) {

		dp[u]=max(dp[u],r[u][i]+i+1);

	}

}

int main() {

	scanf("%d",&n);

	rep(i,2,n+1) {

		scanf("%d",f+i);

		s[f[i]].pb(i);

	}

	dfs(1);

	printf("%d\n",dp[1]);

}


// Faster C++ solution for the same problem (measured speedup: 1.7995863292071566x):
#include<iostream>

#include<cstdio>

#include<queue>

#include<vector>

#include<algorithm>

using namespace std;

int n;

int ans;

int par[100010];

int s[100010];

int z[100010];

int h[100010];

vector<int>v[100010];

void solve(int x)

{

	vector<int>t;

	for(int i=0;i<v[x].size();i++)

	{

		solve(v[x][i]);

		t.push_back(h[v[x][i]]);

	}

	sort(t.begin(),t.end());

	int l=t.size();

	for(int i=0;i<l;i++)

	{

		h[x]=max(h[x],t[i]+l-i);

	}

}

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)h[i]=1; 

	for(int i=2;i<=n;i++)

	{

		scanf("%d",par+i);

		v[par[i]].push_back(i);

	}

	solve(1);

	printf("%d",h[1]-1);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int memo[100001];

int sagasu(int A) {

	if (memo[A]) return memo[A];

	ce(A);

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 1;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return memo[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1) - 1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.441267541763939x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int memo[100001];

int sagasu(int A) {

	if (memo[A]) return memo[A];

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 1;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return memo[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1) - 1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.384721629375554x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int sagasu(int A) {

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 0;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1));



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.425578916018674x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int memo[100001];

int sagasu(int A) {

	if (memo[A]) return memo[A];

	ce(A);

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 1;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return memo[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1) - 1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.441267541763939x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	if (n[A] == n[A - 1]) {

		return sagasu(E[H[A]].first) + 1;

	}

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int memo[100001];

int sagasu(int A) {

	if (memo[A]) return memo[A];

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 1;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return memo[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1) - 1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.384721629375554x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	if (n[A] == n[A - 1]) {

		return sagasu(E[H[A]].first) + 1;

	}

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int sagasu(int A) {

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 0;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1));



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.425578916018674x):
//#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	if (n[A] == n[A - 1]) {

		return sagasu(E[H[A]].first) + 1;

	}

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



pair<int, int> E[100001];

int H[100001];

int memo[100001];

int sagasu(int A) {

	if (memo[A]) return memo[A];

	ce(A);

	vector<int> kari;

	for (int i = H[A]; i; i = E[i].second) kari.pb(sagasu(E[i].first));

	sort(kari.begin(), kari.end(), greater<int>());

	int ret = 1;

	rep(i, kari.size()) ret = max(ret, i + kari[i] + 1);

	return memo[A] = ret;

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		E[i] = mp(i, H[a]);

		H[a] = i;

	}



	co(sagasu(1) - 1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.4439369820852415x):
#pragma GCC optimize ("O3")

//#pragma GCC target ("avx")

//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

#include<functional>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define chmin(x, y) x = min(x, y)

#define chmax(x, y) x = max(x, y)

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

pair<int, int> E[100001];

int H[100001];

int n[100001], kari[100001];

int sagasu(int A) {

	if (n[A] == n[A - 1]) return 0;

	if (n[A] == n[A - 1] + 1) {

		return sagasu(E[H[A]].first) + 1;

	}

	int k = n[A - 1];

	for (int i = H[A]; i; i = E[i].second) {

		kari[k++] = sagasu(E[i].first);

	}

	sort(kari + n[A - 1], kari + n[A], greater<int>());

	int ret = 0;

	for (int i = 0; i < n[A] - n[A - 1]; i++) ret = max(ret, kari[n[A - 1] + i] + i);

	return ret + 1;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	for (int i = 2; i <= N; i++) {

		int a = getint();

		n[a]++;

		E[i] = mp(i, H[a]);

		H[a] = i;

	}

	rep1(i, N) n[i] = n[i - 1] + n[i];



	printf("%d\n", sagasu(1));



	Would you please return 0;

}