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

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int main()
{
	for(int n;cin>>n && n;){
		vi left(n-1),right(n-1);
		rep(i,n-1){
			cin>>left[i]>>right[i];
			left[i]--,right[i]--;
		}
		
		vi ps(n,-1);
		rep(i,n-1){
			if(left[i]!=n-1) ps[left[i]]=i;
			if(right[i]!=n-1) ps[right[i]]=i;
		}
		
		vi depth(n);
		repi(i,1,n-1)
			depth[i]=1+depth[ps[i]];
		
		vi size(n,1);
		per(i,n-1)
			size[i]=1+size[left[i]]+size[right[i]];
		
		vl unsaved(n);
		per(i,n-1){
			int l=left[i],r=right[i];
			unsaved[i]=unsaved[l]+(size[l]+1)/2+unsaved[r]+(size[r]+1)/2;
		}
		
		vl saved(n);
		per(i,n-1){
			int l=left[i],r=right[i];
			ll a=unsaved[i];
			ll b=1+saved[l]+depth[i]+1+saved[r];
			ll c=unsaved[l]+(size[l]+1)/2+1+saved[r];
			ll d=unsaved[r]+(size[r]+1)/2+1+saved[l];
			saved[i]=min({a,b,c,d});
		}
		
		cout<<saved[0]<<endl;
	}
}