#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int pr[100009], sz[100009], d[100009];
vector<int> ch[100009];

void dfs(int n)
{
	vector<int> v;
	int s = 0;
	sz[n] = 1; d[n] = 1;
	for (int i = 0; i < ch[n].size(); i++) {
		int tn = ch[n][i];
		dfs(tn);
		sz[n] += sz[tn];
		d[n] += sz[tn] - d[tn];
		int t = 2 * d[tn] - sz[tn];
		if (sz[tn] & 1) v.push_back(t);
		else if (t < 0) d[n] += t;
		else s += t;
	}
	if (v.size() % 2 == 0) d[n] += s;
	sort(v.begin(), v.end()); 
	for (int i = 0; i < v.size(); i += 2)
		d[n] += v[i];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	for (int i = 2; i <= n; i++) {
		cin >> pr[i];
		ch[pr[i]].push_back(i);
	}
	dfs(1);
	cout << d[1] << '\n';
	return 0;
}
#include <bits/stdc++.h>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
#define FOR(i,a,b)  for(int i=(a); i< (b); ++i)
#define RFOR(i,b,a) for(int i=(b);i>=(a);i--)
#define REP(i,a,b)  for(int i=(a); i<=(b); ++i)
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
template<typename T>
void PrArr(const T a[] , int len){
	for(int i = 0;i <= len; i++)cout << a[i] << " ";
	cout << endl;
}
template<typename T>
void PrVec(const vector<T> a){
	for(auto it : a)cout << it << " ";
	cout << endl;
}
const int MAX = 0x7ffffff;
const int MIN = 0xcf;
int test;
void slove()
{
	ll n, x = 1, t, len = 0; 
	cin >> n;
	t = n;
	while(t >= 10){
		t /= 10;x *= 10;len ++;
	} len ++;
	ll ans = (n - x + 1) * ((len / 3) - (len % 3 == 0));
	
	t = 0, n = 1, len = 0;
	while(t <= x)
	{
		t = t * 10 + 9;
		len ++;
		if(t > x)break;
		ans += (t - n + 1)* ((len / 3) - (len % 3 == 0));
		//cout << t  << " " << n << " " << len << endl;
		n = n * 10;
	}
	
	cout << ans << endl;
}
int main()
{
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed; // 在iomanip中
#endif
	SIS;slove();
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}