n = gets.to_i
g = {}
(1..n).map {|i| g[i] = []}
depth = {1 => 0}
gets.split.map(&:to_i).each_with_index do |x, i|
	g[x] << i+2
	depth[i+2] = depth[x] + 1
end

bfs_order = []
queue = [1]
idx = 0
until queue.empty?
	u = queue.shift
	bfs_order << u
	g[u].each {|v| queue << v}
end

def compute_euler_tour_index(graph, u, next_idx, et)
	et[u] = [next_idx]
	next_idx += 1
	graph[u].each do |v|
		next_idx = compute_euler_tour_index(graph, v, next_idx, et)
		et[u] << next_idx
		next_idx += 1
	end
	return next_idx
end

et = {}
compute_euler_tour_index(g, 1, 0, et)

ary = []
et.each do |k, is|
	is.each do |i|
		ary[i] = depth[k]
	end
end

class SegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			@min = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@right_child = self.class.new(arr, c+1, r)
			lm = @left_child.min
			rm = @right_child.min
			@min = (lm < rm ? lm : rm)
		end
	end

	def query(l, r)
		return @min if l == @l && r == @r
		if r <= @left_child.r
			return @left_child.query(l, r)
		elsif @right_child.l <= l
			return @right_child.query(l, r)
		else
			lm = @left_child.query(l, @left_child.r)
			rm = @right_child.query(@right_child.l, r)
			return lm < rm ? lm : rm
		end
	end

	attr_reader :min, :l, :r
end

tree = SegmentTree.new(ary)

total_dist = 0
bfs_order.each_cons(2) do |u, v|
	is = [et[u][0], et[u][-1], et[v][0], et[v][-1]].sort
	lca_depth = tree.query(is[0], is[-1])
	total_dist += (ary[et[u][0]] + ary[et[v][0]] - lca_depth * 2)
end
p total_dist