Example

Input

4
1 1 2


Output

6
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

class Graph{
	private:
	int n;
	vvi g;
	int lv=20;
	vi rank;
	vvi Par;
	void LCA_dfs(int v,int t){
		rank[v]=t;
		for(auto u:g[v]) if(rank[u]==-1){
			Par[0][u]=v;
			LCA_dfs(u,t+1);
		}
	}
	public:
	void LCA_Prepare(){
		rank=vi(n,-1);
		Par=vvi(lv,vi(n));
		Par[0][0]=-1;
		LCA_dfs(0,0);
		for(int i=1;i<lv;i++) for(int j=0;j<n;j++){
			if(Par[i-1][j]<0) Par[i][j]=-1;
			else Par[i][j]=Par[i-1][Par[i-1][j]];
		}
	}
	int LCA(int s,int t){
		if(rank[s]>rank[t]) swap(s,t);
		for(int i=0;i<lv;i++) if((rank[t]-rank[s])>>i&1) t=Par[i][t];
		if(s==t) return s;
		for(int i=lv-1;i>=0;i--) if(Par[i][s]!=Par[i][t]){
			s=Par[i][s];
			t=Par[i][t];
		}
		return Par[0][s];
	}
	Graph(int v){
		n=v;
		g=vvi(v);
	}
	void add_edge(int s,int t){
		g[s].push_back(t);
		g[t].push_back(s);
	}
	int dis(int u,int v){
		return rank[u]+rank[v]-2*rank[LCA(u,v)];
	}
};

int n;
vvi a;

int main(){
	cin>>n;
	a=vvi(n);
	Graph g(n);
	for(int i=1;i<n;i++){
		int x;
		cin>>x;
		x--;
		g.add_edge(x,i);
		a[x].push_back(i);
	}
	g.LCA_Prepare();
	queue<int> q;
	q.push(0);
	ll res=0,pre=0;
	for(int i=0;i<n;i++){
		int v=q.front();q.pop();
		res+=g.dis(pre,v);
		pre=v;
		for(auto u:a[v]) q.push(u);
	}
	cout<<res<<endl;
}
