Example

Input

4
1 1 2


Output

6
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int n;
vector<int> g[111111];
vector<int> vs;
int dep[111111],par[22][111111];

void dfs(int v,int pre,int d){
  dep[v]=d;
  par[0][v]=pre;
  for(int nv: g[v]){
    if(nv!=pre){
      dfs(nv,v,d+1);
    }
  }
}

int lca(int u,int v){
  if(dep[u]>dep[v])swap(u,v);
  rep(k,20){
    if((dep[v]-dep[u])>>k&1){
      v=par[k][v];
    }
  }
  if(u==v)return v;
  for(int k=20-1;k>=0;k--){
    if(par[k][u]!=par[k][v]){
      u=par[k][u]; v=par[k][v];
    }
  }
  return par[0][u];
}

int main(){
	cin.sync_with_stdio(false);
  cin>>n;
  rep(i,n-1){
    int par;
    cin>>par; par--;
    g[par].push_back(i+1); g[i+1].push_back(par);
  }

  dfs(0,-1,0);
  for(int k=0;k+1<20;k++){
    for(int v=0;v<n;v++){
      if(par[k][v]<0)par[k+1][v]=-1;
      else par[k+1][v]=par[k][par[k][v]];
    }
  }

  queue<int> que;
  que.push(0);
  while(que.size()){
    int v=que.front(); que.pop();
    vs.push_back(v);
    for(int nv : g[v]){
      if(dep[nv]>dep[v])que.push(nv);
    }
  }

  ll res=0;
  rep(i,vs.size()-1){
    int u=vs[i],v=vs[i+1];
    res+=dep[u]+dep[v]-dep[lca(u,v)]*2;
  }
  cout<<res<<endl;
	return 0;
}