Example

Input

4
1 1 2


Output

6
#include<algorithm>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-8;

const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
bool chmax(T &a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T &a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

int n;
vector< vector<int> > g;
int root = 0;

int parent[20][100000];
int depth[100000];

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    for(int i = 0 ; i < g[v].size() ; ++i){
        if(g[v][i] != p){
            dfs(g[v][i], v, d + 1);
        }
    }
}

void init(int v){
    dfs(root, -1, 0);

    for(int k = 0 ; k + 1 < 20 ; ++k){
        for(int i = 0 ; i < v ; ++i){
            if(parent[k][i] < 0){
                parent[k + 1][i] = -1;
            }else{
                parent[k + 1][i] = parent[k][parent[k][i]];
            }
        }
    }
}

int lca(int u, int v){
    if(depth[u] > depth[v]){
        swap(u, v);
    }
    for(int k = 0 ; k < 20 ; ++k){
        if((depth[v] - depth[u]) >> k & 1){
            v = parent[k][v];
        }
    }
    if(u == v){
        return u;
    }
    for(int k = 19 ; k >= 0 ; --k){
        if(parent[k][u] != parent[k][v]){
            u = parent[k][u];
            v = parent[k][v];
        }
    }

    return parent[0][u];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;

    g.resize(n);
    reps(i, n - 1){
        int p;
        cin >> p;
        g[--p].emplace_back(i);
    }

    init(n);

    lint ans = 0;
    int prev = 0;
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int now = que.front();
        que.pop();
        int p = lca(now, prev);
        ans += depth[now] + depth[prev] - 2LL * depth[p];
        prev = now;
        for(auto to : g[now]){
            que.push(to);
        }
    }

    cout << ans << endl;

    return 0;
}
