Example

Input

4
1 1 2


Output

6
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
struct aa {
	int dep;
	int id;
	int panum;
	int from;
};
bool operator<(const aa&l, const aa&r) {
	return l.dep == r.dep ? l.panum == r.panum ? l.id < r.id : l.panum < r.panum : l.dep < r.dep;
}
class Compare {
public:
	//aa?????????????????¶
	bool operator()(const aa&l, const aa&r) {
			return l.dep == r.dep ? l.panum == r.panum ? l.id >r.id : l.panum> r.panum : l.dep > r.dep;

	}
};
class Tree {
public:
	Tree(int V, int root) : V(V), root(root), cnum(V), place(V), id(V) {
		T.resize(V);
		for (int i = 0; i < MAXLOGV; i++) {
			parent[i].resize(V);
		}
		depth.resize(V);
	}
	// u??¨v????????????
	// lca????±????????????¨????????????????????§????????°????????¨????????????
	void unite(int u, int v) {
		T[u].push_back(v);
		T[v].push_back(u);
	}
	// init??????
	// ????????????????????????????????????????????????????????°????????¨lca????±???????????????????
	void init() {
		dfs(root, 0, 0);
		int id = 0;
		getid(root, 0, id);
	}
	// u??¨v???lca????±???????
	int lca(int u, int v) const {
		if (depth[u] > depth[v]) swap(u, v);
		for (int k = 0; k < MAXLOGV; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = MAXLOGV - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
	// u??¨v????????¢????±???????
	// edge????????????????????¨????????????????????????????????????
	int dist(int u, int v) const {
		int p = lca(u, v);
		return (depth[u] - depth[p]) + (depth[v] - depth[p]);
	}
	int dfs(int v, int p, int d) {
		parent[0][v] = p;
		depth[v] = d;
		cnum[v] = 0;
		for (int i = 1; i < MAXLOGV; i++) {
			parent[i][v] = parent[i - 1][parent[i - 1][v]];
		}
		for (int next : T[v]) {
			if (next != p) cnum[v] += dfs(next, v, d + 1);
		}
		return cnum[v] + 1;
	}
	vector<int> bfs() {
		vector<int>perms(V);
		priority_queue<aa, vector<aa>, Compare>que;
		que.push(aa{ 0,0,-1,-1 });
		int num = 0;
		while (!que.empty()) {
			aa atop(que.top());
			que.pop();
			perms[atop.id] = num;
			num++;
			for (int next : T[atop.id]) {
				if (next != atop.from) {
					que.push(aa{ depth[next],next,num-1,atop.id });
				}
			}
		}
		vector<int>aa(V);
		for (int i = 0; i < perms.size(); ++i) {
			aa[perms[i]] = i;
		}
		return aa;
	}

	void getid(const int v, const int p, int &nplace) {
		place[v] = nplace;
		id[nplace] = v;
		nplace++;
		for (int next : T[v]) {
			if (next != p) getid(next, v, nplace);
		}
	}
	static const int MAXLOGV = 25;
	// ??°???????????£??\???????????¨???
	vector<vector<int> > T;
	// ???????????°
	int V;
	// ?????????????????????
	int root;

	// ????????????
	vector<int> parent[MAXLOGV];
	// ??????????????±???
	vector<int> depth;

	//????????°
	vector<int>cnum;

	//??????
	vector<int>place;
	vector<int>id;

};

int main() {
	int N; cin >> N;
	Tree t(N, 0);
	for (int i = 0; i < N-1; ++i) {
		int a; cin >> a;
		t.unite(i+1, a - 1);
	}
	t.init();
	long long int ans = 0;
	vector<int>aa(t.bfs());
	for (int i = 0; i < N - 1; ++i) {
		ans += t.dist(aa[i],aa[i+1]);
	}
	cout << ans << endl;
	return 0;
}