#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define stream istringstream
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repv(i,n) for(int i=n-1; i>=0; i--)
#define repl(i,n) for(int i=1; i<=(int)n; i++)
#define replv(i,n) for(int i=n; i>=1; i--)

#define INF (1<<28)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define sdi(x) scanf("%d",&x)
#define sdii(x,y) scanf("%d%d",&x,&y)
#define SDs(x) scanf("%s",x)
#define uu first
#define vv second

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=n;i<=k+1;j--,i++){
            if(i!=j){
                vct.pb(j);
                vct.pb(i);
            }
            else{
                vct.pb(i);
            }
        }
        for(int i=k+1;i<=n;i++){
            vct.pb(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.pb(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define stream istringstream
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repv(i,n) for(int i=n-1; i>=0; i--)
#define repl(i,n) for(int i=1; i<=(int)n; i++)
#define replv(i,n) for(int i=n; i>=1; i--)

#define INF (1<<28)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define sdi(x) scanf("%d",&x)
#define sdii(x,y) scanf("%d%d",&x,&y)
#define SDs(x) scanf("%s",x)
#define uu first
#define vv second

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=k+1 ;i<=(k/2)+1;j--,i++){
            if(i!=j){
                vct.pb(j);
                vct.pb(i);
            }
            else{
                vct.pb(i);
            }
        }
        if(n-k>1)
        for(int i=k+2;i<=n;i++){
            vct.pb(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.pb(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define stream istringstream
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repv(i,n) for(int i=n-1; i>=0; i--)
#define repl(i,n) for(int i=1; i<=(int)n; i++)
#define replv(i,n) for(int i=n; i>=1; i--)

#define INF (1<<28)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define sdi(x) scanf("%d",&x)
#define sdii(x,y) scanf("%d%d",&x,&y)
#define SDs(x) scanf("%s",x)
#define uu first
#define vv second

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=k ;i<=(k/2);j--,i++){
            if(i!=j){
                vct.pb(j);
                vct.pb(i);
            }
            else{
                vct.pb(i);
            }
        }
        for(int i=k+1;i<=n;i++){
            vct.pb(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.pb(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define stream istringstream
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repv(i,n) for(int i=n-1; i>=0; i--)
#define repl(i,n) for(int i=1; i<=(int)n; i++)
#define replv(i,n) for(int i=n; i>=1; i--)

#define INF (1<<28)
#define PI 3.14159265358979323846264338327950
#define pb(x) push_back(x)
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define pii pair<int,int>
#define pmp make_pair

#define sdi(x) scanf("%d",&x)
#define sdii(x,y) scanf("%d%d",&x,&y)
#define SDs(x) scanf("%s",x)
#define uu first
#define vv second

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=k;i<=k/2;j--,i++){
            if(i!=j){
                vct.pb(j);
                vct.pb(i);
            }
            else{
                vct.pb(i);
            }
        }
        for(int i=k+1;i<=n;i++){
            vct.pb(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.pb(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


#include <cstdio>
#include <vector>
#include <iostream>

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=k+1 ;i<=(k/2)+1;j--,i++){
            if(i!=j){
                vct.push_back(j);
                vct.push_back(i);
            }
            else{
                vct.push_back(i);
            }
        }
        if(n-k>1)
        for(int i=k+2;i<=n;i++){
            vct.push_back(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.push_back(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


#include <cstdio>
#include <vector>
#include <iostream>

using namespace std;
vector<int>vct;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    long long n,k;
    scanf("%lld %lld", &n, &k);

    if(k>1){
        int x=k-1;

        for(int i=1, j=k+1 ;i<=(k/2)+1;j--,i++){
            if(i!=j){
                vct.pb(j);
                vct.pb(i);
            }
            else{
                vct.pb(i);
            }
        }
        if(n-k>1)
        for(int i=k+2;i<=n;i++){
            vct.pb(i);
        }

    }
    else{
        for(int i=1; i<=n;i++){

            vct.pb(i);

        }
    }

    for(int i=0; i<n;i++){
        printf("%d ", vct[i]);
    }

return 0;
}


// In The Name Of God

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+22 ;

int n,t,root;
int num[N],l[N],r[N];
bool rot[N];
map<int,bool>p;
set<int>sl,sr;
set<int>:: iterator it;

void dfs(int v)
{
	int x1,x2;
	it=(sl.begin());
	x1=(*it);
	it=(sr.end());
	it--;
	x2=(*it);
	if(num[v]<=x1 && num[v]>=x2) p[num[v]]=true;
	if(l[v]!=-1)
	{
		sl.insert(num[v]);
		dfs(l[v]);
		sl.erase(num[v]);
	}
	if(r[v]!=-1)
	{
		sr.insert(num[v]);
		dfs(r[v]);
		sr.erase(num[v]);
	}
}

int main()
{
	cin>>n;
	memset(rot,true,sizeof(rot));
	for(int i=1;i<=n;i++)
	{
		cin>>num[i]>>l[i]>>r[i];
		if(l[i]!=-1) rot[l[i]]=false;
		if(r[i]!=-1) rot[r[i]]=false;
	}
	for(int i=1;i<=n;i++)
		if(rot[i]) root=i;
	sl.insert(2e9);
	sr.insert(-1);

	dfs(root);
	for(int i=1;i<=n;i++)
		if(p[num[i]]) t++;
	cout<<n-t;

	return 0;
}


// In The Name Of God

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+22 ;

int n,t,root;
int num[N],l[N],r[N];
bool rot[N];
set<int>sl,sr;
set<int>:: iterator it;

void dfs(int v)
{
	int x1,x2;
	it=(sl.begin());
	x1=(*it);
	it=(sr.end());
	it--;
	x2=(*it);
	if(num[v]<x1 && num[v]>x2) t++;
	if(l[v]!=-1)
	{
		sl.insert(num[v]);
		dfs(l[v]);
		sl.erase(num[v]);
	}
	if(r[v]!=-1)
	{
		sr.insert(num[v]);
		dfs(r[v]);
		sr.erase(num[v]);
	}
}

int main()
{
	cin>>n;
	memset(rot,true,sizeof(rot));
	for(int i=1;i<=n;i++)
	{
		cin>>num[i]>>l[i]>>r[i];
		if(l[i]!=-1) rot[l[i]]=false;
		if(r[i]!=-1) rot[r[i]]=false;
	}
	for(int i=1;i<=n;i++)
		if(rot[i]) root=i;
	sl.insert(2e9);
	sr.insert(-1);

	dfs(root);
	cout<<n-t;

	return 0;
}


// In The Name Of God

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+22 ;

int n,t,root;
int num[N],l[N],r[N];
bool rot[N];
map<int,bool>p;
multiset<int>sl,sr;
multiset<int>:: iterator it;

void dfs(int v)
{
	int x1,x2;
	it=(sl.begin());
	x1=(*it);
	it=(sr.end());
	it--;
	x2=(*it);
	if(num[v]<=x1 && num[v]>=x2) p[num[v]]=true;
	if(l[v]!=-1)
	{
		sl.insert(num[v]);
		dfs(l[v]);
		sl.erase(sl.find(num[v]));
	}
	if(r[v]!=-1)
	{
		sr.insert(num[v]);
		dfs(r[v]);
		sr.erase(sr.find(num[v]));
	}
}

int main()
{
	cin>>n;
	memset(rot,true,sizeof(rot));
	for(int i=1;i<=n;i++)
	{
		cin>>num[i]>>l[i]>>r[i];
		if(l[i]!=-1) rot[l[i]]=false;
		if(r[i]!=-1) rot[r[i]]=false;
	}
	for(int i=1;i<=n;i++)
		if(rot[i]) root=i;
	sl.insert(2e9);
	sr.insert(-1);

	dfs(root);
	for(int i=1;i<=n;i++)
		if(p[num[i]]) t++;
	cout<<n-t;

	return 0;
}


// In The Name Of God

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5+22 ;

int n,t,root;
int num[N],l[N],r[N];
bool rot[N];
set<int>sl,sr;
set<int>:: iterator it;

void dfs(int v)
{
	int x1,x2;
	it=(sl.begin());
	x1=(*it);
	it=(sr.end());
	it--;
	x2=(*it);
	if(num[v]<=x1 && num[v]>=x2) t++;
	if(l[v]!=-1)
	{
		sl.insert(num[v]);
		dfs(l[v]);
		sl.erase(num[v]);
	}
	if(r[v]!=-1)
	{
		sr.insert(num[v]);
		dfs(r[v]);
		sr.erase(num[v]);
	}
}

int main()
{
	cin>>n;
	memset(rot,true,sizeof(rot));
	for(int i=1;i<=n;i++)
	{
		cin>>num[i]>>l[i]>>r[i];
		if(l[i]!=-1) rot[l[i]]=false;
		if(r[i]!=-1) rot[r[i]]=false;
	}
	for(int i=1;i<=n;i++)
		if(rot[i]) root=i;
	sl.insert(2e9);
	sr.insert(-1);

	dfs(root);
	cout<<n-t;

	return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define mp(x1, x2) make_pair(x1, x2)
#define pb(x1, x2) push_back(x1, x2)
#define pii pair<int, int>
#define x first
#define y second
#define vpii vector<pair<int, int> >
#define rep(p,a,b) for(int p = a; p <= b; p++)
#define per(p,a,b) for(int p = a; p >= b; p--)

using namespace std;

struct node
{
	node *prawy, *lewy, *ojciec;
	int val;
};

const int duzo = 100007;
bool czy[duzo];
node bst[duzo];
set<int> vals;
set<int> rozne;

void dfs(node *w, int a, int b)
{
	vals.insert((*w).val);
	if((*w).lewy != NULL && (*w).lewy->val < (*w).val && (*w).lewy->val < b && (*w).lewy->val > a)
	{
		dfs((*w).lewy, a, (*w).val);
	}
	if((*w).prawy != NULL && (*w).prawy->val > (*w).val && (*w).prawy->val < b && (*w).prawy->val > a)
	{
		dfs((*w).prawy, (*w).val, b);
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	rep(i,1,n)
	{
		bst[i].lewy = NULL;
		bst[i].prawy = NULL;
		bst[i].ojciec = NULL;
	}
	rep(i,1,n)
	{
		int l, r, v;
		scanf("%d%d%d", &v, &l, &r);
		bst[i].val = v;
		rozne.insert(v);
		if(l != -1)
		{
			bst[i].lewy = &bst[l];
			bst[l].ojciec = &bst[i];
		}
		if(r != -1)
		{
			bst[i].prawy = &bst[r];
			bst[r].ojciec = &bst[i];
		}
	}
	int root;
	rep(i,1,n)
	{
		if(bst[i].ojciec == NULL)
		{
			root = i;
			break;
		}
	}
	dfs(&bst[root], -1, 1000000007);
	int s = rozne.size() - vals.size();
	printf("%d", s);
	return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define mp(x1, x2) make_pair(x1, x2)
#define pb(x1, x2) push_back(x1, x2)
#define pii pair<int, int>
#define x first
#define y second
#define vpii vector<pair<int, int> >
#define rep(p,a,b) for(int p = a; p <= b; p++)
#define per(p,a,b) for(int p = a; p >= b; p--)

using namespace std;

struct node
{
	node *prawy, *lewy, *ojciec;
	int val;
};

const int duzo = 100007;
bool czy[duzo];
node bst[duzo];
int wart[duzo];
map<int, int> m;
int ile[duzo];

void dfs(node *w, int a, int b)
{
	czy[m[(*w).val]] = true;
	if((*w).lewy != NULL && (*w).lewy->val < (*w).val && (*w).lewy->val < b && (*w).lewy->val > a)
	{
		dfs((*w).lewy, a, (*w).val);
	}
	if((*w).prawy != NULL && (*w).prawy->val > (*w).val && (*w).prawy->val < b && (*w).prawy->val > a)
	{
		dfs((*w).prawy, (*w).val, b);
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	rep(i,1,n)
	{
		bst[i].lewy = NULL;
		bst[i].prawy = NULL;
		bst[i].ojciec = NULL;
	}
	rep(i,1,n)
	{
		int l, r, v;
		scanf("%d%d%d", &v, &l, &r);
		bst[i].val = v;
		wart[i-1] = v;
		if(l != -1)
		{
			bst[i].lewy = &bst[l];
			bst[l].ojciec = &bst[i];
		}
		if(r != -1)
		{
			bst[i].prawy = &bst[r];
			bst[r].ojciec = &bst[i];
		}
	}
	sort(wart, wart+n);
	int ost = -1;
	int l = -1;
	rep(i,0,n-1)
	{
		if(wart[i] != ost)
		{
			ost = wart[i];
			m[wart[i]] = ++l;
		}
		ile[l]++;
	}
	int root;
	rep(i,1,n)
	{
		if(bst[i].ojciec == NULL)
		{
			root = i;
			break;
		}
	}
	dfs(&bst[root], -1, 1000000007);
	int rekt = 0;
	rep(i,0,l)
	{
		if(czy[i])
		{
			rekt += ile[i];
		}
	}
	printf("%d", n - rekt);
	return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define mp(x1, x2) make_pair(x1, x2)
#define pb(x1, x2) push_back(x1, x2)
#define pii pair<int, int>
#define x first
#define y second
#define vpii vector<pair<int, int> >
#define rep(p,a,b) for(int p = a; p <= b; p++)
#define per(p,a,b) for(int p = a; p >= b; p--)

using namespace std;

struct node
{
	node *prawy, *lewy, *ojciec;
	int val;
};

const int duzo = 100007;
bool czy[duzo];
node bst[duzo];
int wart[duzo];
int p[duzo];
map<int, int> m;
int ile[duzo];

void dfs(node *w, int a, int b)
{
	czy[m[(*w).val]] = true;
	if((*w).lewy != NULL && (*w).lewy->val < (*w).val && (*w).lewy->val < b && (*w).lewy->val > a)
	{
		dfs((*w).lewy, a, (*w).val);
	}
	if((*w).prawy != NULL && (*w).prawy->val > (*w).val && (*w).prawy->val < b && (*w).prawy->val > a)
	{
		dfs((*w).prawy, (*w).val, b);
	}
}

int main()
{
	int n;
	scanf("%d", &n);
	rep(i,1,n)
	{
		bst[i].lewy = NULL;
		bst[i].prawy = NULL;
		bst[i].ojciec = NULL;
	}
	rep(i,1,n)
	{
		int l, r, v;
		scanf("%d%d%d", &v, &l, &r);
		bst[i].val = v;
		wart[i-1] = v;
		if(l != -1)
		{
			bst[i].lewy = &bst[l];
			bst[l].ojciec = &bst[i];
		}
		if(r != -1)
		{
			bst[i].prawy = &bst[r];
			bst[r].ojciec = &bst[i];
		}
	}
	sort(wart, wart+n);
	int ost = -1;
	int l = -1;
	rep(i,0,n-1)
	{
		if(wart[i] != ost)
		{
			ost = wart[i];
			p[++l] = wart[i];
			m[wart[i]] = l;
		}
		ile[l]++;
	}
	int root;
	rep(i,1,n)
	{
		if(bst[i].ojciec == NULL)
		{
			root = i;
			break;
		}
	}
	dfs(&bst[root], -1, 1000000007);
	int rekt = 0;
	rep(i,0,l)
	{
		if(czy[i])
		{
			rekt += ile[i];
		}
	}
	printf("%d", n - rekt);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int duzo = 100007, inf = 2e9+17;
int le[duzo], pr[duzo], v[duzo], par[duzo];
int ile;

void dfs(int w, int nnle = inf, int gzpr = -1) {
	if(v[w] > nnle) ++ile;
	else if(v[w] < gzpr) ++ile;
	if(le[w] != -1) dfs(le[w], min(nnle, v[w]), gzpr);
	if(pr[w] != -1) dfs(pr[w], nnle, max(gzpr, v[w]));
}

int main() {
	ios_base::sync_with_stdio(0);
	int n, root;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> v[i] >> le[i] >> pr[i];
		par[le[i]] = par[pr[i]] = i;
	}
	for(int i = 1; i <= n; ++i) if(!par[i]) root = i;
	dfs(root);
	cout << ile;
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int duzo = 100007, inf = 2e9+17;
int le[duzo], pr[duzo], v[duzo], par[duzo];
set<int> dob;

void dfs(int w, int nnle = inf, int gzpr = -1) {
	bool cok = true;
	if(v[w] > nnle) cok = false;
	else if(v[w] < gzpr) cok = false;
	if(cok) dob.insert(v[w]);
	if(le[w] != -1) dfs(le[w], min(nnle, v[w]), gzpr);
	if(pr[w] != -1) dfs(pr[w], nnle, max(gzpr, v[w]));
}

int main() {
	ios_base::sync_with_stdio(0);
	int n, root;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> v[i] >> le[i] >> pr[i];
		par[le[i]] = par[pr[i]] = i;
	}
	for(int i = 1; i <= n; ++i) if(!par[i]) root = i;
	dfs(root);
	int ile = 0;
	for(int i = 1; i <= n; ++i) if(dob.count(v[i]) == 1) ++ile;
	cout << n - ile;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

const int MAXN = 112345;
int n;
int value[MAXN];
bool not_reached[MAXN];
int left_child[MAXN], right_child[MAXN];
int indeg[MAXN];
vector <int> subtree_sorted[MAXN];
int root_tree;

void merge(int parent, int left, int right) {
    int esq = 0, dir = 0;
    int tam_l = subtree_sorted[left].size();
    int tam_r = subtree_sorted[right].size();

    bool parent_processed = false;
    while (esq < tam_l || dir < tam_r || !parent_processed) {
        int esq_idx = (esq >= tam_l) ? -1 : subtree_sorted[left][esq];
        int dir_idx = (dir >= tam_r) ? -1 : subtree_sorted[right][dir];

        if (!parent_processed && (esq_idx == -1 || value[parent] < value[esq_idx]) &&
            (dir_idx == -1 || value[parent] < value[dir_idx])) {
            parent_processed = true;
            subtree_sorted[parent].push_back(parent);
        }
        else if (dir_idx == -1 || (esq_idx != -1 && value[esq_idx] <= value[dir_idx])) {
            if (value[esq_idx] > value[parent]) {
                not_reached[esq_idx] = true;
            }
            subtree_sorted[parent].push_back(esq_idx);
            esq++;
        } else {
            if (value[dir_idx] < value[parent]) {
                not_reached[dir_idx] = true;
            }
            subtree_sorted[parent].push_back(dir_idx);
            dir++;
        }
    }
}

void solve(int v) {
    if (v == -1) {
        return;
    }
    solve(left_child[v]);
    solve(right_child[v]);
    merge(v, left_child[v], right_child[v]);
}

void build_tree() {
    for (int i = 1; i <= n; i++) {
        scanf("%d", &value[i]);
        scanf("%d %d", &left_child[i], &right_child[i]);
        indeg[left_child[i]]++;
        indeg[right_child[i]]++;
        subtree_sorted[i].clear();
    }
    for (int i = 1; i <= n; i++) {
        if (indeg[i] == 0) {
            root_tree = i;
        }
    }
}

int main() {
    scanf("%d", &n);
    build_tree();
    solve(root_tree);
    int counter = 0;
    for (int i = 1; i <= n; i++) {
        counter += not_reached[i];
    }
    printf("%d\n", counter);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

const int MAXN = 112345;
int n;
int value[MAXN];
bool not_reached[MAXN];
int left_child[MAXN], right_child[MAXN];
int indeg[MAXN];
vector <int> subtree_sorted[MAXN];
int root_tree;

void merge(int parent, int left, int right) {
    int esq = 0, dir = 0;
    int tam_l = subtree_sorted[left].size();
    int tam_r = subtree_sorted[right].size();

    bool parent_processed = false;
    while (esq < tam_l || dir < tam_r || !parent_processed) {
        int esq_idx = (esq >= tam_l) ? -1 : subtree_sorted[left][esq];
        int dir_idx = (dir >= tam_r) ? -1 : subtree_sorted[right][dir];

        if (!parent_processed && (esq_idx == -1 || value[parent] < value[esq_idx]) &&
            (dir_idx == -1 || value[parent] < value[dir_idx])) {
            parent_processed = true;
            subtree_sorted[parent].push_back(parent);
        }
        else if (dir_idx == -1 || (esq_idx != -1 && value[esq_idx] <= value[dir_idx])) {
            if (value[esq_idx] > value[parent]) {
                not_reached[esq_idx] = true;
            }
            subtree_sorted[parent].push_back(esq_idx);
            esq++;
        } else {
            if (value[dir_idx] < value[parent]) {
                not_reached[dir_idx] = true;
            }
            subtree_sorted[parent].push_back(dir_idx);
            dir++;
        }
    }

    subtree_sorted[left].clear();
    subtree_sorted[right].clear();
}

void solve(int v) {
    if (v == -1) {
        return;
    }
    solve(left_child[v]);
    solve(right_child[v]);
    merge(v, left_child[v], right_child[v]);
}

void build_tree() {
    for (int i = 1; i <= n; i++) {
        scanf("%d", &value[i]);
        scanf("%d %d", &left_child[i], &right_child[i]);
        indeg[left_child[i]]++;
        indeg[right_child[i]]++;
        subtree_sorted[i].clear();
    }
    for (int i = 1; i <= n; i++) {
        if (indeg[i] == 0) {
            root_tree = i;
        }
    }
}

int main() {
    scanf("%d", &n);
    build_tree();
    solve(root_tree);
    int counter = 0;
    for (int i = 1; i <= n; i++) {
        counter += not_reached[i];
    }
    printf("%d\n", counter);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

typedef pair<int, int> P;

const int MAXN = 112345;
int n;
int value[MAXN];
bool not_reached[MAXN];
int left_child[MAXN], right_child[MAXN];
int indeg[MAXN];
int pointer_to_set[MAXN];
set <P> subtree[MAXN];
set <int> values_found;
int root_tree;

void merge(int parent, int left, int right) {
    vector <P> aux;

    if (left != -1) {
        left = pointer_to_set[left];
        set<P>::iterator it_left = subtree[left].upper_bound(make_pair(value[parent], MAXN));
        aux.clear();
        while (it_left != subtree[left].end()) {
            aux.push_back(*it_left);
            not_reached[(*it_left).second] = true;
            it_left++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[left].erase(*it);
        }
    }

    if (right != -1) {
        right = pointer_to_set[right];
        set<P>::iterator it_right = subtree[right].begin();
        aux.clear();
        while (it_right != subtree[right].end() && (*it_right).first < value[parent]) {
            aux.push_back(*it_right);
            not_reached[(*it_right).second] = true;
            it_right++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[right].erase(*it);
        }
    }

    if (left == -1) {
        subtree[right].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = right;
    } else if (right == -1) {
        subtree[left].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = left;
    }

    else {
        int tam_l = subtree[left].size();
        int tam_r = subtree[right].size();
        if (tam_l < tam_r) {
            swap(left, right);
            swap(tam_l, tam_r);
        }
        for (set<P>::iterator it = subtree[right].begin(); it != subtree[right].end(); it++) {
            subtree[left].insert(*it);
        }
        subtree[right].clear();
        subtree[left].insert(make_pair(value[parent], parent));

        pointer_to_set[parent] = left;
    }
}

void solve(int v) {
    if (v == -1) {
        return;
    }
    if (left_child[v] == -1 && right_child[v] == -1) {
        subtree[v].insert(make_pair(value[v], v));
        pointer_to_set[v] = v;
        return;
    }
    solve(left_child[v]);
    solve(right_child[v]);
    merge(v, left_child[v], right_child[v]);
}

void build_tree() {
    for (int i = 1; i <= n; i++) {
        scanf("%d", &value[i]);
        scanf("%d %d", &left_child[i], &right_child[i]);
        indeg[left_child[i]]++;
        indeg[right_child[i]]++;
        subtree[i].clear();
    }
    for (int i = 1; i <= n; i++) {
        if (indeg[i] == 0) {
            root_tree = i;
        }
    }
}

int main() {
    scanf("%d", &n);
    build_tree();
    solve(root_tree);
    int counter = 0;
    for (int i = 1; i <= n; i++) {
        if (!not_reached[i]) {
            values_found.insert(value[i]);
        }
    }
    for (int i = 1; i <= n; i++) {
        counter += (values_found.find(value[i]) == values_found.end());
    }
    printf("%d\n", counter);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

typedef pair<int, int> P;

const int MAXN = 112345;
int n;
int value[MAXN];
bool not_reached[MAXN];
int left_child[MAXN], right_child[MAXN];
int indeg[MAXN];
int pointer_to_set[MAXN];
set <P> subtree[MAXN];
int root_tree;

void merge(int parent, int left, int right) {
    vector <P> aux;

    if (left != -1) {
        left = pointer_to_set[left];
        set<P>::iterator it_left = subtree[left].upper_bound(make_pair(value[parent], MAXN));
        aux.clear();
        while (it_left != subtree[left].end()) {
            aux.push_back(*it_left);
            not_reached[(*it_left).second] = true;
            it_left++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[left].erase(*it);
        }
    }

    if (right != -1) {
        right = pointer_to_set[right];
        set<P>::iterator it_right = subtree[right].begin();
        aux.clear();
        while (it_right != subtree[right].end() && (*it_right).first < value[parent]) {
            aux.push_back(*it_right);
            not_reached[(*it_right).second] = true;
            it_right++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[right].erase(*it);
        }
    }

    if (left == -1) {
        subtree[right].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = right;
    } else if (right == -1) {
        subtree[left].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = left;
    }

    else {
        int tam_l = subtree[left].size();
        int tam_r = subtree[right].size();
        if (tam_l < tam_r) {
            swap(left, right);
            swap(tam_l, tam_r);
        }
        for (set<P>::iterator it = subtree[right].begin(); it != subtree[right].end(); it++) {
            subtree[left].insert(*it);
        }
        subtree[right].clear();
        subtree[left].insert(make_pair(value[parent], parent));

        pointer_to_set[parent] = left;
    }
}

void solve(int v) {
    if (v == -1) {
        return;
    }
    if (left_child[v] == -1 && right_child[v] == -1) {
        subtree[v].insert(make_pair(value[v], v));
        pointer_to_set[v] = v;
        return;
    }
    solve(left_child[v]);
    solve(right_child[v]);
    merge(v, left_child[v], right_child[v]);
}

void build_tree() {
    for (int i = 1; i <= n; i++) {
        scanf("%d", &value[i]);
        scanf("%d %d", &left_child[i], &right_child[i]);
        indeg[left_child[i]]++;
        indeg[right_child[i]]++;
        subtree[i].clear();
    }
    for (int i = 1; i <= n; i++) {
        if (indeg[i] == 0) {
            root_tree = i;
        }
    }
}

int main() {
    scanf("%d", &n);
    build_tree();
    solve(root_tree);
    int counter = 0;
    for (int i = 1; i <= n; i++) {
        counter += not_reached[i];
    }
    printf("%d\n", counter);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

typedef pair<int, int> P;

const int MAXN = 112345;
int n;
int value[MAXN];
bool not_reached[MAXN];
int left_child[MAXN], right_child[MAXN];
int indeg[MAXN];
int pointer_to_set[MAXN];
set <P> subtree[MAXN];
int root_tree;

void merge(int parent, int left, int right) {
    vector <P> aux;

    if (left != -1) {
        left = pointer_to_set[left];
        set<P>::iterator it_left = subtree[left].upper_bound(make_pair(value[parent], parent));
        aux.clear();
        while (it_left != subtree[left].end()) {
            aux.push_back(*it_left);
            not_reached[(*it_left).second] = true;
            it_left++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[left].erase(*it);
        }
    }

    if (right != -1) {
        right = pointer_to_set[right];
        set<P>::iterator it_right = subtree[right].begin();
        aux.clear();
        while (it_right != subtree[right].end() && (*it_right).first < value[parent]) {
            aux.push_back(*it_right);
            not_reached[(*it_right).second] = true;
            it_right++;
        }
        for (vector<P>::iterator it = aux.begin(); it != aux.end(); it++) {
            subtree[right].erase(*it);
        }
    }

    if (left == -1) {
        subtree[right].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = right;
    } else if (right == -1) {
        subtree[left].insert(make_pair(value[parent], parent));
        pointer_to_set[parent] = left;
    }

    else {
        int tam_l = subtree[left].size();
        int tam_r = subtree[right].size();
        if (tam_l < tam_r) {
            swap(left, right);
            swap(tam_l, tam_r);
        }
        for (set<P>::iterator it = subtree[right].begin(); it != subtree[right].end(); it++) {
            subtree[left].insert(*it);
        }
        subtree[right].clear();
        subtree[left].insert(make_pair(value[parent], parent));

        pointer_to_set[parent] = left;
    }
}

void solve(int v) {
    if (v == -1) {
        return;
    }
    if (left_child[v] == -1 && right_child[v] == -1) {
        subtree[v].insert(make_pair(value[v], v));
        pointer_to_set[v] = v;
        return;
    }
    solve(left_child[v]);
    solve(right_child[v]);
    merge(v, left_child[v], right_child[v]);
}

void build_tree() {
    for (int i = 1; i <= n; i++) {
        scanf("%d", &value[i]);
        scanf("%d %d", &left_child[i], &right_child[i]);
        indeg[left_child[i]]++;
        indeg[right_child[i]]++;
        subtree[i].clear();
    }
    for (int i = 1; i <= n; i++) {
        if (indeg[i] == 0) {
            root_tree = i;
        }
    }
}

int main() {
    scanf("%d", &n);
    build_tree();
    solve(root_tree);
    int counter = 0;
    for (int i = 1; i <= n; i++) {
        counter += not_reached[i];
    }
    printf("%d\n", counter);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+5,M = 320;

int sz[N],l[N],r[N],val[N];

void dfs(int x){
    sz[x]=1;
    if(l[x]!=-1 && sz[l[x]]==0)
        dfs(l[x]);
    if(l[x]!=-1)sz[x]+=sz[l[x]];

    if(r[x]!=-1 && sz[r[x]]==0)
        dfs(r[x]);

    if(r[x]!=-1)sz[x]+=sz[r[x]];
}

map<int,bool > Hash;

void dfs2(int x,int L,int R){
    if(L<= val[x] && val[x]<=R)
        Hash[val[x]]=1;

    if(l[x]!=-1){
        dfs2(l[x], L, min(R,val[x]-1));
    }
    if(r[x]!=-1)
        dfs2(r[x], max(L,val[x]),R);
}
int main(){
    int n,i,root;
    cin>>n;
    for(i=1;i<=n;++i)
        scanf("%d%d%d",&val[i],&l[i],&r[i]),Hash[val[i]]=0;

    memset(sz,0,sizeof(sz));
    for(i=1;i<=n;++i)
        if(sz[i]==0){
            dfs(i);
            if(sz[i]==n)root=i;
            break;
        }
    dfs2(root,0,1e9);
    int ret=0;
    for(i=1;i<=n;++i)
        if(!Hash[val[i]])++ret;
    printf("%d\n",ret);
}


	#include <stdio.h>
	#include <queue>
	#include <math.h>
	#include <iostream>
	#include <assert.h>
	#include <string>
	#include <algorithm>
	#include <vector>
	#include <set>
	#include <map>
	#include <memory.h>
	#include <fstream>
	#include <functional>

	using namespace std;

	const int N = 1e5 + 5, M = 320;

	int sz[N], l[N], r[N], val[N];

	void dfs(int x) {
		sz[x] = 1;
		if (l[x] != -1 && sz[l[x]] == 0)
			dfs(l[x]);
		if (l[x] != -1)sz[x] += sz[l[x]];

		if (r[x] != -1 && sz[r[x]] == 0)
			dfs(r[x]);

		if (r[x] != -1)sz[x] += sz[r[x]];
	}

	map<int, bool > Hash;

	void dfs2(int x, int L, int R) {
		if (L <= val[x] && val[x] <= R)
			Hash[val[x]] = 1;

		if (l[x] != -1) {
			dfs2(l[x], L, min(R, val[x] - 1));
		}
		if (r[x] != -1)
			dfs2(r[x], max(L, val[x]), R);
	}

	bool is[N];
	int main() {
		int n, i, root;
		cin >> n;
		for (i = 1; i <= n; ++i) {
			scanf("%d%d%d", &val[i], &l[i], &r[i]), Hash[val[i]] = 0;

			if (l[i] != -1)is[l[i]] = 1;
			if (r[i] != -1)is[r[i]] = 1;
		}
		for (i = 1; i <= n; ++i)if (!is[i])root = i;
		root = -1;
		for (i = 1; i <= n; ++i) {

			if (sz[i] == 0) {
				dfs(i);
				if (sz[i] == n)root = i;
			}
		}
		dfs2(root, 0, 1e9);

		int ret = 0;
		for (i = 1; i <= n; ++i)
			if (!Hash[val[i]])++ret;
		printf("%d\n", ret);
	}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+5,M = 320;

int sz[N],l[N],r[N],val[N];

void dfs(int x){
    sz[x]=1;
    if(l[x]!=-1 && sz[l[x]]==0)
        dfs(l[x]);
    if(l[x]!=-1)sz[x]+=sz[l[x]];
    if(r[x]!=-1 && sz[r[x]]==0)
        dfs(r[x]),sz[x]+=sz[r[x]];

    if(r[x]!=-1)sz[x]+=sz[r[x]];
}
map<int,bool > Hash;

void dfs2(int x,int L,int R){
    if(L<= val[x] && val[x]<=R)
        Hash[val[x]]=1;

    if(l[x]!=-1){
        dfs2(l[x], L, min(R,val[x]-1));
    }
    if(r[x]!=-1)
        dfs2(r[x], max(L,val[x]),R);
}
int main(){
    int n,i,root;
    cin>>n;
    for(i=1;i<=n;++i)
        scanf("%d%d%d",&val[i],&l[i],&r[i]);
    memset(sz,0,sizeof(sz));
    for(i=1;i<=n;++i)
        if(sz[i]==0){
            dfs(i);
            if(sz[i]==n)root=i;
            break;
        }
    dfs2(root,0,1e9);
    int ret=0;
    for(i=1;i<=n;++i)
        if(!Hash[val[i]])++ret;
    printf("%d\n",ret);
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+5,M = 320;

int sz[N],l[N],r[N],val[N];

void dfs(int x){
    sz[x]=1;
    if(l[x]!=-1 && sz[l[x]]==0)
        dfs(l[x]);
    if(l[x]!=-1)sz[x]+=sz[l[x]];
    if(r[x]!=-1 && sz[r[x]]==0)
        dfs(r[x]),sz[x]+=sz[r[x]];

    if(r[x]!=-1)sz[x]+=sz[r[x]];
}
map<int,bool > Hash;

void dfs2(int x,int L,int R){
    if(L<= val[x] && val[x]<=R)
        Hash[val[x]]=1;

    if(l[x]!=-1){
        dfs2(l[x], L, min(R,val[x]-1));
    }
    if(r[x]!=-1)
        dfs2(r[x], max(L,val[x]),R);
}
int main(){
    int n,i,root;
    cin>>n;
    for(i=1;i<=n;++i)
        scanf("%d%d%d",&val[i],&l[i],&r[i]),Hash[val[i]]=0;

    memset(sz,0,sizeof(sz));
    for(i=1;i<=n;++i)
        if(sz[i]==0){
            dfs(i);
            if(sz[i]==n)root=i;
            break;
        }
    if(val[1]==911605217)cout<<root<<endl;
    dfs2(root,0,1e9);
    int ret=0;
    for(i=1;i<=n;++i)
        if(!Hash[val[i]])++ret;
    printf("%d\n",ret);
}


#include <stdio.h>
#include <queue>
#include <math.h>
#include <iostream>
#include <assert.h>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <memory.h>
#include <fstream>
#include <functional>

using namespace std;

const int N = 1e5 + 5, M = 320;

int sz[N], l[N], r[N], val[N];

void dfs(int x) {
	sz[x] = 1;
	if (l[x] != -1 && sz[l[x]] == 0)
		dfs(l[x]);
	if (l[x] != -1)sz[x] += sz[l[x]];

	if (r[x] != -1 && sz[r[x]] == 0)
		dfs(r[x]);

	if (r[x] != -1)sz[x] += sz[r[x]];
}

map<int, bool > Hash;

void dfs2(int x, int L, int R) {
	if (L <= val[x] && val[x] <= R)
		Hash[val[x]] = 1;

	if (l[x] != -1) {
		dfs2(l[x], L, min(R, val[x] - 1));
	}
	if (r[x] != -1)
		dfs2(r[x], max(L, val[x]), R);
}

bool is[N];
int main() {
	int n, i, root;
	cin >> n;
	for (i = 1; i <= n; ++i) {
		scanf("%d%d%d", &val[i], &l[i], &r[i]), Hash[val[i]] = 0;

		if (l[i] != -1)is[l[i]] = 1;
		if (r[i] != -1)is[r[i]] = 1;
	}
	for (i = 1; i <= n; ++i)if (!is[i])root = i;
	root = -1;
	for (i = 1; i <= n; ++i) {

		if (sz[i] == 0) {
			dfs(i);
			if (sz[i] == n)root = i;
		}
	}if (val[1] == 911605217)cout << sz[1] << endl;
	dfs2(root, 0, 1e9);

	int ret = 0;
	for (i = 1; i <= n; ++i)
		if (!Hash[val[i]])++ret;
	printf("%d\n", ret);
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+5,M = 320;

int sz[N],l[N],r[N],val[N];

void dfs(int x){
    sz[x]=1;
    if(l[x]!=-1 && sz[l[x]]==0)
        dfs(l[x]);
    if(l[x]!=-1)sz[x]+=sz[l[x]];

    if(r[x]!=-1 && sz[r[x]]==0)
        dfs(r[x]);

    if(r[x]!=-1)sz[x]+=sz[r[x]];
}

map<int,bool > Hash;

void dfs2(int x,int L,int R){
    if(L<= val[x] && val[x]<=R)
        Hash[val[x]]=1;

    if(l[x]!=-1){
        dfs2(l[x], L, min(R,val[x]-1));
    }
    if(r[x]!=-1)
        dfs2(r[x], max(L,val[x]),R);
}
int main(){
    int n,i,root;
    cin>>n;
    for(i=1;i<=n;++i)
        scanf("%d%d%d",&val[i],&l[i],&r[i]),Hash[val[i]]=0;

    memset(sz,0,sizeof(sz));
    for(i=1;i<=n;++i)
        if(sz[i]==0){
            dfs(i);
            if(sz[i]==n)root=i;
            break;
        }
    if(val[1]==911605217)cout<<sz[1]<<endl;
    dfs2(root,0,1e9);
    int ret=0;
    for(i=1;i<=n;++i)
        if(!Hash[val[i]])++ret;
    printf("%d\n",ret);
}


#include <bits/stdc++.h>

using namespace std;

struct A {
    int v;
    int l, r;
} node[110000<<2];

int n;
int deg[110000];
int cnt = 0;

void dfs(int u, int mi, int mx) {
    if(node[u].v < mi && node[u].v > mx)
        cnt++;
    if(node[u].l != -1)
        dfs(node[u].l, min(mi, node[u].v), mx);
    if(node[u].r != -1)
        dfs(node[u].r, mi, max(mx, node[u].v));
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &node[i].v, &node[i].l, &node[i].r);
        if(node[i].l != -1) deg[node[i].l] ++;
        if(node[i].r != -1) deg[node[i].r] ++;
    }
    int root = 0;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 0)
            root = i;
    }
    dfs(root, 1e9+1, -1);
    cout<<n - cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct A {
    int v;
    int l, r;
} node[110000<<2];

set<int> S;
int n;
int deg[110000];
int cnt = 0;

void dfs(int u, int mi, int mx) {
    if(node[u].v <= mi && node[u].v >= mx)
        cnt++;
    if(node[u].l != -1)
        dfs(node[u].l, min(mi, node[u].v), mx);
    if(node[u].r != -1)
        dfs(node[u].r, mi, max(mx, node[u].v));
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &node[i].v, &node[i].l, &node[i].r);
        if(node[i].l != -1) deg[node[i].l] ++;
        if(node[i].r != -1) deg[node[i].r] ++;
    }
    int root = 0;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 0)
            root = i;
    }
    dfs(root, 1e9+1, -1);
//    for(int i = 1; i <= n; i++)
//        if(S.find(node[i].v) != S.end())
//            cnt++;
    cout<<n - cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct A {
    int v;
    int l, r;
} node[110000<<2];

set<int> S;
int n;
int deg[110000];
int cnt = 0;

void dfs(int u, int mi, int mx) {
    if(node[u].v < mi && node[u].v > mx)
        S.insert(node[u].v);
    if(node[u].l != -1)
        dfs(node[u].l, min(mi, node[u].v), mx);
    if(node[u].r != -1)
        dfs(node[u].r, mi, max(mx, node[u].v));
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &node[i].v, &node[i].l, &node[i].r);
        if(node[i].l != -1) deg[node[i].l] ++;
        if(node[i].r != -1) deg[node[i].r] ++;
    }
    int root = 0;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 0)
            root = i;
    }
    dfs(root, 1e9+1, -1);
    for(int i = 1; i <= n; i++)
        if(S.find(node[i].v) != S.end())
            cnt++;
    cout<<n - cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
依据伪代码，我们找一个点的时候其实只能取找一个值，
能找到那个值，说明我们就有那个点。
所以即使找到的不是我们真正想找的那个点也可以。
*/

struct A {
    int v;
    int l, r;
} node[110000<<2];

set<int> S;
int n;
int deg[110000];
int cnt = 0;

void dfs(int u, int mi, int mx) {
    if(node[u].v < mi && node[u].v > mx)
        S.insert(node[u].v);
    if(node[u].l != -1)
        dfs(node[u].l, min(mi, node[u].v), mx);
    if(node[u].r != -1)
        dfs(node[u].r, mi, max(mx, node[u].v));
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &node[i].v, &node[i].l, &node[i].r);
        if(node[i].l != -1) deg[node[i].l] ++;
        if(node[i].r != -1) deg[node[i].r] ++;
    }
    int root = 0;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 0)
            root = i;
    }
    dfs(root, 1e9+1, -1);
    for(int i = 1; i <= n; i++)
        if(S.find(node[i].v) != S.end())
            cnt++;
    cout<<n - cnt<<endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

const int maxn = 1e9 + 1;
const int infy = -1;
#define LEFT	0x000001
#define RIGHT	0x000002
#define ALL		0x000003

typedef struct sta {
	int node;
	int max;
	int inf;
	int step;
}sta;

sta stack[10000];
int rear = 0;

typedef struct node {
	int val;
	int L;
	int R;
}node;

node a[1000000];
bool to[1000000];

map<int, bool> m;

inline int read() {
	int p = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return f * p;
}

void find(int u, int maxx, int mmin) {
	if (u == -1) return;

	if (a[u].val >maxx && a[u].val <mmin )
		m[a[u].val] = true;

	find(a[u].L, maxx, min(mmin, a[u].val));
	find(a[u].R, max(maxx, a[u].val), mmin);
}

void new_find(int node, int maxx, int inf) {
	sta& temp = stack[0];
	temp.node = node;
	temp.max  = maxx;
	temp.inf  = inf;
	temp.step = 0;

	while (rear != -1) {
		sta& text = stack[rear];
		if (text.step == ALL|| text.node == -1) {
			rear--;
			continue;
		}

		if (text.step == 0 && a[text.node].val > text.inf&& a[text.node].val < text.max)
			m[a[text.node].val] = true;

		int j = 0;
		for(int i=1;i<=0x000002;i<<=1)
			if (!(text.step & i)) {
				text.step |= i;
				j = i;
				break;
			}

		sta newnode;
		newnode.step = 0;

		switch (j) {
		case LEFT:
			newnode.node = a[text.node].L;
			newnode.max  = min(text.max, a[text.node].val);
			newnode.inf  = text.inf;
			break;
		case RIGHT:
			newnode.node = a[text.node].R;
			newnode.max = text.max;
			newnode.inf = max(a[text.node].val, text.inf);
			break;
		}
		stack[++rear] = newnode;
	}
}

int main() {
	int n;
	n = read();
	for (int i = 1;i <= n;i++) {
		a[i].val = read();
		a[i].L     = read();
		a[i].R     = read();

		if (a[i].L != -1) to[a[i].L] = true;
		if (a[i].R != -1) to[a[i].R] = true;
	}

	int root = 0;
	for(int i=1;i<=n;i++)
		if (!to[i]) {
			root = i;
			break;
		}

	//find(root, -1, maxn);
	new_find(root, maxn, -1);

	int ans = 0;
	for (int i = 1;i <= n;i++)
		if (!m[a[i].val])
			ans++;

	printf("%d", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
const int inf = 0x3f3f3f3f;
struct node{
	int v,l,r;
}tree[N];
int n,rt,ans;
bool book[N];
map<int,bool>p;
void dfs(int now,int Max,int Min){
	if(now==-1)return;
	if(tree[now].v>Max && tree[now].v<Min)p[tree[now].v] = true;
	dfs(tree[now].l,Max,min(Min,tree[now].v));
	dfs(tree[now].r,max(Max,tree[now].v),Min);
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%d%d%d",&tree[i].v,&tree[i].l,&tree[i].r);
		if(~tree[i].l)book[tree[i].l] = true;
		if(~tree[i].r)book[tree[i].r] = true;
	}
	//先找到根
	for(int i = 1;i <= n;i++)if(!book[i]){rt = i;break;}
	dfs(rt,-1,inf);
	for(int i = 1;i <= n;i++)if(!p[tree[i].v])++ans;
	printf("%d",ans);
	return 0;
}


#include<cstdio>
#include<map>
int n,ans=0;
struct tree
{
	std::map<int,int> mp;
	struct node
	{
		int ls,rs,val,fa,sz;
	};
	node f[100005];
	int rt;
	void calc(int v)
	{
		if (f[v].ls) calc(f[v].ls);
		if (f[v].rs) calc(f[v].rs);
		f[v].sz=1+f[f[v].ls].sz+f[f[v].rs].sz;
	}
	void dfs(int v,int l,int r)
	{
		int s=0;
		if (l<=f[v].val&&f[v].val<=r) mp[f[v].val]=1;
		if (f[v].ls) dfs(f[v].ls,l,f[v].val-1);
		if (f[v].rs) dfs(f[v].rs,f[v].val+1,r);
	}
	int solve()
	{
		int ans=0;
		dfs(rt,0,1000000000);
		for (int i=1;i<=n;i++) ans=ans+!mp[f[i].val];
		return ans;
	}
};
tree t;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int v,l,r;
		scanf("%d%d%d",&v,&l,&r);
		if (l!=-1)
		{
			t.f[i].ls=l;
			t.f[l].fa=i;
		}
		if (r!=-1)
		{
			t.f[i].rs=r;
		    t.f[r].fa=i;
		}
		t.f[i].val=v;
	}
	for (int i=1;i<=n;i++) if (!t.f[i].fa) t.rt=i;
	t.calc(t.rt);
	ans=t.solve();
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

const int maxn = 1e7 + 1;
const int infy = -1;
#define LEFT	0x000001
#define RIGHT	0x000002
#define ALL		0x000003

typedef struct sta {
	int node;
	int max;
	int inf;
	int step;
}sta;

sta stack[maxn];
int rear = 0;

typedef struct node {
	int value;
	int L;
	int R;
}node;

node arr[maxn];
bool maps[maxn];

map<int, bool> ins;

inline int read() {
	int p = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return f * p;
}

void find(int node, int maxx, int inf) {
	if (node == -1) return;

	if (arr[node].value > inf&& arr[node].value < maxx)
		ins[arr[node].value] = true;

	find(arr[node].L, min(maxx, arr[node].value), inf);
	find(arr[node].R, maxx, max(inf, arr[node].value));
}

void new_find(int node, int maxx, int inf) {
	sta& temp = stack[0];
	temp.node = node;
	temp.max  = maxx;
	temp.inf  = inf;
	temp.step = 0;

	while (rear != -1) {
		sta& text = stack[rear];
		if (text.step == ALL|| text.node == -1) {
			rear--;
			continue;
		}

		if (text.step == 0 && arr[text.node].value > text.inf&& arr[text.node].value < text.max)
			ins[arr[text.node].value] = true;

		int j = 0;
		for(int i=1;i<=0x000002;i<<=1)
			if (!(text.step & i)) {
				text.step |= i;
				j = i;
				break;
			}

		sta newnode;
		newnode.step = 0;

		switch (j) {
		case LEFT:
			newnode.node = arr[text.node].L;
			newnode.max  = min(text.max, arr[text.node].value);
			newnode.inf  = text.inf;
			break;
		case RIGHT:
			newnode.node = arr[text.node].R;
			newnode.max = text.max;
			newnode.inf = max(arr[text.node].value, text.inf);
			break;
		}
		stack[++rear] = newnode;
	}
}

int main() {
	int n;
	n = read();
	for (int i = 1;i <= n;i++) {
		arr[i].value = read();
		arr[i].L     = read();
		arr[i].R     = read();

		if (arr[i].L != -1) maps[arr[i].L] = true;
		if (arr[i].R != -1) maps[arr[i].R] = true;
	}

	int root = 0;
	for(int i=1;i<=n;i++)
		if (!maps[i]) {
			root = i;
		}

	new_find(root, maxn, infy);

	int ans = 0;
	for (int i = 1;i <= n;i++)
		if (!ins[arr[i].value])
			ans++;

	printf("%d", ans);

	return 0;
}


#include<cstdio>
#include<map>
int n,ans=0;
struct tree
{
	std::map<int,int> mp;
	struct node
	{
		int ls,rs,val,fa,sz;
	};
	node f[100005];
	int rt;
	void calc(int v)
	{
		if (f[v].ls) calc(f[v].ls);
		if (f[v].rs) calc(f[v].rs);
		f[v].sz=1+f[f[v].ls].sz+f[f[v].rs].sz;
	}
	void dfs(int v,int l,int r)
	{
//		printf("dfs %d %d %d\n",v,l,r);
		int s=0;
		if (l<=f[v].val&&f[v].val<=r) mp[f[v].val]=1;
		if (f[v].ls) dfs(f[v].ls,l,std::min(r,f[v].val-1));
		if (f[v].rs) dfs(f[v].rs,std::max(l,f[v].val+1),r);
	}
	int solve()
	{
//		printf("%d\n",rt);
		int ans=0;
		dfs(rt,0,1000000000);
		for (int i=1;i<=n;i++) ans=ans+!mp[f[i].val];
//		,printf("%d %d\n",i,mp[f[i].val]);
		return ans;
	}
};
tree t;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int v,l,r;
		scanf("%d%d%d",&v,&l,&r);
		if (l!=-1)
		{
			t.f[i].ls=l;
			t.f[l].fa=i;
		}
		if (r!=-1)
		{
			t.f[i].rs=r;
		    t.f[r].fa=i;
		}
		t.f[i].val=v;
	}
	for (int i=1;i<=n;i++) if (!t.f[i].fa) t.rt=i;
	t.calc(t.rt);
	ans=t.solve();
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int N = 100010;
const int inf = 0x3f3f3f3f;
struct node {
	int v, l, r;
}tree[N];
int n, rt, ans;
bool book[N];
map<int, bool>p;
void dfs(int now, int Max, int Min) {
	if (now == -1)return;
	if (tree[now].v > Max&& tree[now].v < Min)p[tree[now].v] = true;
	dfs(tree[now].l, Max, min(Min, tree[now].v));
	dfs(tree[now].r, max(Max, tree[now].v), Min);
}
int main()
{
	scanf("%d", &n);
	for (int i = 1;i <= n;i++) {
		scanf("%d%d%d", &tree[i].v, &tree[i].l, &tree[i].r);
		if (~tree[i].l)book[tree[i].l] = true;
		if (~tree[i].r)book[tree[i].r] = true;
	}
	//先找到根
	for (int i = 1;i <= n;i++)if (!book[i]) { rt = i;break; }
	dfs(rt, -1, inf);
	for (int i = 1;i <= n;i++)if (!p[tree[i].v])++ans;
	printf("%d", ans);
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

const int maxn = 1e5 + 1;
const int infy = -1;
#define LEFT	0x000001
#define RIGHT	0x000002
#define ALL		0x000003

typedef struct sta {
	int node;
	int max;
	int inf;
	int step;
}sta;

sta stack[maxn];
int rear = 0;

typedef struct node {
	int value;
	int L;
	int R;
}node;

node arr[maxn];
bool maps[maxn];

map<int, bool> ins;

inline int read() {
	int p = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return f * p;
}

void find(int node, int maxx, int inf) {
	if (node == -1) return;

	if (arr[node].value > inf&& arr[node].value < maxx)
		ins[arr[node].value] = true;

	find(arr[node].L, min(maxx, arr[node].value), inf);
	find(arr[node].R, maxx, max(inf, arr[node].value));
}

void new_find(int node, int maxx, int inf) {
	sta& temp = stack[0];
	temp.node = node;
	temp.max  = maxx;
	temp.inf  = inf;
	temp.step = 0;

	while (rear != -1) {
		sta& text = stack[rear];
		if (text.step == ALL|| text.node == -1) {
			rear--;
			continue;
		}

		if (text.step == 0 && arr[text.node].value > text.inf&& arr[text.node].value < text.max)
			ins[arr[text.node].value] = true;

		int j = 0;
		for(int i=1;i<=0x000002;i<<=1)
			if (!(text.step & i)) {
				text.step |= i;
				j = i;
				break;
			}

		sta newnode;
		newnode.step = 0;

		switch (j) {
		case LEFT:
			newnode.node = arr[text.node].L;
			newnode.max  = min(text.max, arr[text.node].value);
			newnode.inf  = text.inf;
			break;
		case RIGHT:
			newnode.node = arr[text.node].R;
			newnode.max = text.max;
			newnode.inf = max(arr[text.node].value, text.inf);
			break;
		}
		stack[++rear] = newnode;
	}
}

int main() {
	int n;
	n = read();
	for (int i = 1;i <= n;i++) {
		arr[i].value = read();
		arr[i].L     = read();
		arr[i].R     = read();

		if (arr[i].L != -1) maps[arr[i].L] = true;
		if (arr[i].R != -1) maps[arr[i].R] = true;
	}

	int root = 0;
	for(int i=1;i<=n;i++)
		if (!maps[i]) {
			root = i;
		}

	new_find(root, maxn, infy);

	int ans = 0;
	for (int i = 1;i <= n;i++)
		if (!ins[arr[i].value])
			ans++;

	printf("%d", ans);

	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

const int maxn = 1e7 + 1;
const int infy = -1;
#define LEFT	0x000001
#define RIGHT	0x000002
#define ALL		0x000003

typedef struct sta {
	int node;
	int max;
	int inf;
	int step;
}sta;

sta stack[1000000];
int rear = 0;

typedef struct node {
	int value;
	int L;
	int R;
}node;

node arr[maxn];
bool maps[maxn];

map<int, bool> ins;

inline int read() {
	int p = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return f * p;
}

void new_find(int node, int maxx, int inf) {
	sta& temp = stack[0];
	temp.node = node;
	temp.max  = maxx;
	temp.inf  = inf;
	temp.step = 0;

	while (rear != -1) {
		sta& text = stack[rear];
		if (text.step == ALL|| text.node == -1) {
			rear--;
			continue;
		}

		if (text.step == 0 && arr[text.node].value > text.inf&& arr[text.node].value < text.max)
			ins[arr[text.node].value] = true;

		int j = 0;
		for(int i=1;i<=0x000002;i<<=1)
			if (!(text.step & i)) {
				text.step |= i;
				j = i;
				break;
			}

		sta newnode;
		newnode.step = 0;

		switch (j) {
		case LEFT:
			newnode.node = arr[text.node].L;
			newnode.max  = min(text.max, arr[text.node].value);
			newnode.inf  = text.inf;
			break;
		case RIGHT:
			newnode.node = arr[text.node].R;
			newnode.max = text.max;
			newnode.inf = max(arr[text.node].value, text.inf);
			break;
		}
		stack[++rear] = newnode;
	}
}

int main() {
	int n;
	n = read();
	for (int i = 1;i <= n;i++) {
		arr[i].value = read();
		arr[i].L     = read();
		arr[i].R     = read();

		if (arr[i].L != -1) maps[arr[i].L] = true;
		if (arr[i].R != -1) maps[arr[i].R] = true;
	}

	int root = 0;
	for(int i=1;i<=n;i++)
		if (!maps[i]) {
			root = i;
		}

	new_find(root, maxn, infy);

	int ans = 0;
	for (int i = 1;i <= n;i++)
		if (!ins[arr[i].value])
			ans++;

	printf("%d", ans);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,rt,a[100010],ls[100010],rs[100010],vis[100010],ans;
map<int,bool>mp;
void f(int nw){if(nw==-1)return;vis[nw]=1;}
void dfs(int nw,int l,int r)
{
    if(nw==-1)return ;
    if(a[nw]>l&&a[nw]<r)mp[a[nw]]=1;
    dfs(ls[nw],l,min(r,a[nw])),dfs(rs[nw],max(l,a[nw]),r);
}
void get_ans(){for(int i=1;i<=n;i++)if(!mp[a[i]])ans++;}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d%d",&a[i],&ls[i],&rs[i]),f(ls[i]),f(rs[i]);
    for(int i=1;i<=n;i++)if(!vis[i])rt=i;
    dfs(rt,-1,1e9+1),get_ans(),printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int lch[maxn],rch[maxn],fa[maxn],val[maxn],ans;
bool vis[maxn];
map<int,bool> yes,no;
int dfs(int u,int l,int r)
{
	if(val[u]<l||val[u]>r) no[val[u]]=true;
	else yes[val[u]]=true;
	if(lch[u]!=-1) dfs(lch[u],l,min(r,val[u]-1));
	if(rch[u]!=-1) dfs(rch[u],max(l,val[u]+1),r);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&val[i],&lch[i],&rch[i]);
		if(lch[i]!=-1) fa[lch[i]]=i;
		if(rch[i]!=-1) fa[rch[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==0)
		{
			dfs(i,-1,1000000007);
			break;
		}
	}

	for(auto it : no)
	{
		//if(!yes[it.first])
		ans++;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int lch[maxn],rch[maxn],fa[maxn],val[maxn],ans;
bool vis[maxn];
int dfs(int u,int l,int r)
{
	//printf("%d\n",val[u]);
	if(val[u]<l||val[u]>r) ++ans;
	if(lch[u]!=-1) dfs(lch[u],l,min(r,val[u]-1));
	if(rch[u]!=-1) dfs(rch[u],max(l,val[u]+1),r);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&val[i],&lch[i],&rch[i]);
		if(lch[i]!=-1) fa[lch[i]]=i;
		if(rch[i]!=-1) fa[rch[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==0)
		{
			dfs(i,-1,1000000007);
			break;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int lch[maxn],rch[maxn],fa[maxn],val[maxn],ans;
bool vis[maxn];
map<int,int> yes,no;
int dfs(int u,int l,int r)
{
	if(val[u]<l||val[u]>r)
	{
		no[val[u]]++;
		ans++;
	}
	else yes[val[u]]=1;
	if(lch[u]!=-1) dfs(lch[u],l,min(r,val[u]-1));
	if(rch[u]!=-1) dfs(rch[u],max(l,val[u]+1),r);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&val[i],&lch[i],&rch[i]);
		if(lch[i]!=-1) fa[lch[i]]=i;
		if(rch[i]!=-1) fa[rch[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==0)
		{
			dfs(i,-1,1000000007);
			break;
		}
	}

	for(auto it : yes)
	{
		ans-=no[it.first];
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int lch[maxn],rch[maxn],fa[maxn],val[maxn],ans;
bool vis[maxn];
map<int,bool> yes,no;
int dfs(int u,int l,int r)
{
	if(val[u]<l||val[u]>r) no[val[u]]=true;
	else yes[val[u]]=true;
	if(lch[u]!=-1) dfs(lch[u],l,min(r,val[u]-1));
	if(rch[u]!=-1) dfs(rch[u],max(l,val[u]+1),r);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&val[i],&lch[i],&rch[i]);
		if(lch[i]!=-1) fa[lch[i]]=i;
		if(rch[i]!=-1) fa[rch[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==0)
		{
			dfs(i,-1,1000000007);
			break;
		}
	}
	for(auto it : no)
	{
		if(!yes[it.first]) ans++;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int lch[maxn],rch[maxn],fa[maxn],val[maxn],ans;
bool vis[maxn];
map<int,bool> yes,no;
int dfs(int u,int l,int r)
{
	if(val[u]<l||val[u]>r) no[val[u]]=true;
	else yes[val[u]]=true;
	if(lch[u]!=-1) dfs(lch[u],l,min(r,val[u]));
	if(rch[u]!=-1) dfs(rch[u],max(l,val[u]),r);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&val[i],&lch[i],&rch[i]);
		if(lch[i]!=-1) fa[lch[i]]=i;
		if(rch[i]!=-1) fa[rch[i]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==0)
		{
			dfs(i,-1,1000000007);
			break;
		}
	}
	for(auto it : no)
	{
		if(!yes[it.first]) ans++;
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
set <int> s;
set <int> all;

void dfs(int v, int mx_l, int mn_r, bool good)
{
    if (v == -1)
    {
        return;
    }
    all.insert(val[v]);
    bool ged = good;
    if (mx_l <= val[v] && mn_r >= val[v] && good)
    {
        ged = true;
        s.insert(val[v]);
    }
    else
    {
        ged = false;
    }
    dfs(r[v], max(mx_l, val[v]), mn_r, ged);
    dfs(l[v], mx_l, min(mn_r, val[v]), ged);
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9, 1);
    cout << (int) all.size() - (int) s.size() << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
bool vis[N];
int cnt;
set <int> s;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    vis[v] = true;
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        s.insert(val[v]);
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (!s.count(val[i]))
        {
            ans++;
        }
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
bool vis[N];
int cnt;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    vis[v] = true;
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        cnt++;
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    cout << n - cnt << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
bool vis[N];
int cnt;
set <int> s;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    vis[v] = true;
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        s.insert(val[v]);
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    cout << n - (int) s.size() << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define x1 X1
#define x2 X2
#define y1 Y1
#define y2 Y2

using namespace std;

typedef long long ll;

const int N = 50 + 1;

int a[N][N];

int dp[N][N][N][N];
bool used[N][N][N][N];
int cnt[N][N];

int get(int x, int y)
{
    if (x < 0 || y < 0)
    {
        return 0;
    }
    return cnt[x][y];
}

int get(int x1, int y1, int x2, int y2)
{
    return get(x2, y2) - get(x2, y1 - 1) - get(x1 - 1, y2) + get(x1 - 1, y1 - 1);
}

inline int rec(int x1, int x2, int y1, int y2)
{
    if (used[x1][x2][y1][y2])
    {
        return dp[x1][x2][y1][y2];
    }
    used[x1][x2][y1][y2] = true;
    int f = get(x1, y1, x2, y2);
    if (x1 == x2 && y1 == y2)
    {
        return 0;
    }
    dp[x1][x2][y1][y2] = 1e9;
    for (int a = x1; a < x2; a++)
    {
        dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], rec(x1, a, y1, y2) + rec(a + 1, x2, y1, y2) + f);
    }
    for (int a = y1; a < y2; a++)
    {
        dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], rec(x1, x2, y1, a) + rec(x1, x2, a + 1, y2) + f);
    }
    return dp[x1][x2][y1][y2];
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            cnt[i][j] = get(i - 1, j) + get(i, j - 1) - get(i - 1, j - 1) + a[i][j];
        }
    }
    cout << rec(0, n - 1, 0, m - 1) << '\n';
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
bool vis[N];
int cnt;
set <int> s;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    vis[v] = true;
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        s.insert(val[v]);
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (!s.count(val[i]))
        {
            ans++;
        }
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
bool vis[N];
set <int> s;
set <int> all;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    vis[v] = true;
    all.insert(val[v]);
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        s.insert(val[v]);
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    for (int i = 1; i <= n; i++)
    {
        assert(vis[i]);
    }
    cout << (int) all.size() - (int) s.size() << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int val[N];
int l[N], r[N];
int par[N];
set <int> s;
set <int> all;

void dfs(int v, int mx_l, int mn_r)
{
    if (v == -1)
    {
        return;
    }
    all.insert(val[v]);
    if (mx_l <= val[v] && mn_r >= val[v])
    {
        s.insert(val[v]);
    }
    dfs(r[v], max(mx_l, val[v]), mn_r);
    dfs(l[v], mx_l, min(mn_r, val[v]));
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int v, a, b;
        scanf("%d%d%d", &v, &a, &b);
        if (a != -1)
        {
            par[a] = i;
        }
        if (b != -1)
        {
            par[b] = i;
        }
        val[i] = v;
        l[i] = a, r[i] = b;
    }
    int r = -1;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == 0)
        {
            r = i;
        }
    }
    dfs(r, 0, 1e9);
    cout << (int) all.size() - (int) s.size() << '\n';
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
struct Node{
  int n,p,li,ri,s;
};
Node node[100001];
int n,root,count = 0;
int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
    }
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  int flag,count = 0;
  int p,t,s;
  for( int i=1; i<=n; ++i )
    {
      t = node[i].n;
      flag = 0;
      p = node[i].p;
      s = node[i].s;
      while( p != -1 )
	if( s == 0 && node[i].n > node[p].n )
	  {
	    ++count;
	    //	    printf("%d\n",i);
	    break;
	  }
        else if( s == 1 && node[i].n <= node[p].n )
	  {
	    ++count;
	    // printf("%d\n",i);
	    break;
	  }
        else
	  {
	    s = node[ p ].s;
	    p = node[ p ].p;
	  }

    }
  printf("%d",count);
  return 0;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  for( int i=0; i<=ndex; ++i)
    if( success[i] == -1 )
      {
  	++ount;
	//		cout<<a[i]<<" "<<success[i]<<"\n";
      }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex+1,mid;
  while( beg <= end )
    {
      mid = (beg+end)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];

  for( int i=0; i<=ndex; ++i )
    success[i] = 0;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  ount = 0;
  for( int i=0; i<=ndex; ++i)
    {
      ount += success[i] != 0?0:1;
      //      cout<<success[i]<<" ";
    }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    ++success[ bns( v.n ) ];
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  // for( int i=0; i<=ndex; ++i)
  //   if( success[i] == -1 )
  //     {
  // 	++ount;
  // 	//		cout<<a[i]<<" "<<success[i]<<"\n";
  //     }
  printf("%d",n - ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n < v.ll || v.n > v.rl ) )
    ++ount;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = 0;
  while( p < t )
    {
      while( p < t && a[++p] == temp );
      a[ ++ndex ] = a[p];
      //  cout<<"u"<<a[ndex];
    }
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  for( int i=0; i<=ndex; ++i)
    if( success[i] == -1 )
      {
  	++ount;
	//		cout<<a[i]<<" "<<success[i]<<"\n";
      }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex+1,mid;
  while( beg <= end )
    {
      mid = (beg+end)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  // for( int i=0; i<=ndex; ++i)
  //   if( success[i] == -1 )
  //     {
  // 	++ount;
  // 	//		cout<<a[i]<<" "<<success[i]<<"\n";
  //     }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n < v.ll || v.n > v.rl ) )
    ++ount;//    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000],cnt[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {
      cnt[++ndex] = 1;
      a[ ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp )
	 ++cnt[ndex];
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];

  for( int i=0; i<=ndex; ++i )
    success[i] = 0;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  ount = 0;
  for( int i=0; i<=ndex; ++i)
    {
      if( success[i] == 0 )
	ount += cnt[i];
      //            cout<<cnt[i]<<" ";
    }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    ++success[ bns( v.n ) ];
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
using namespace std;
struct Node{
  int n,p,li,ri,s;
};
Node node[1000001];
int n,root,count = 0;
int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
    }
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  int flag,count = 0;
  int p,t,s;
  for( int i=1; i<=n; ++i )
    {
      t = node[i].n;
      flag = 0;
      p = node[i].p;
      s = node[i].s;
      while( p != -1 )
	if( s == 0 && node[i].n > node[p].n )
	  {
	    ++count;
	    //	    printf("%d\n",i);
	    break;
	  }
        else if( s == 1 && node[i].n <= node[p].n )
	  {
	    ++count;
	    // printf("%d\n",i);
	    break;
	  }
        else
	  {
	    s = node[ p ].s;
	    p = node[ p ].p;
	  }

    }
  printf("%d",count);
  return 0;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = -1,temp = a[0];
  ndex = 0;
  while( p < t )
    {
      while( p < t && a[++p] == temp );
      a[ ++ndex ] = a[p];
      //  cout<<"u"<<a[ndex];
    }
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  for( int i=0; i<=ndex; ++i)
    if( success[i] == -1 )
      {
  	++ount;
	//		cout<<a[i]<<" "<<success[i]<<"\n";
      }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex+1,mid;
  while( beg <= end )
    {
      mid = (beg+end)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000],cnt[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {
      cnt[++ndex] = 1;
      a[ ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp )
	 ++cnt[ndex];
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    {
      a[++ndex] = a[t];
      cnt[ndex] = 1;
    }

  for( int i=0; i<=ndex; ++i )
    success[i] = 0;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  ount = 0;
  for( int i=0; i<=ndex; ++i)
    {
      if( success[i] == 0 )
	ount += cnt[i];
      //                  cout<<cnt[i]<<" ";
    }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    ++success[ bns( v.n ) ];
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  // for( int i=0; i<=ndex; ++i)
  //   if( success[i] == -1 )
  //     {
  // 	++ount;
  // 	//		cout<<a[i]<<" "<<success[i]<<"\n";
  //     }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n <= v.ll || v.n > v.rl ) )
    ++ount;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  for( int i=0; i<=ndex; ++i)
    if( success[i] == -1 )
      {
  	++ount;
	//		cout<<a[i]<<" "<<success[i]<<"\n";
      }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n <= v.ll || v.n > v.rl ) )
    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
using namespace std;
struct Node{
  int n,p,li,ri;
};
Node node[100001];
int n,root,count = 0;
int dfs(Node v)
{
  return 0;
}

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d",&node[i].n,&node[i].li,&node[i].ri);
      node[ node[i].li ].p = i;
      node[ node[i].ri ].p = i;
    }
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  dfs( node[ root ] );
  return 0;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];

  for( int i=0; i<=ndex; ++i )
    success[i] = 0;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  ount = 0;
  for( int i=0; i<=ndex; ++i)
    {
      ount += success[i];
      //cout<<success[i]<<" ";
    }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n < v.ll || v.n > v.rl ) )
    ++success[ bns( v.n ) ];
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = 0;
  while( p < t )
    {
      while( p < t && a[++p] == temp );
      a[ ++ndex ] = a[p];
      temp = a[p];
      //  cout<<"u"<<a[ndex];
    }
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  //   cout<<"i"<<ndex;
  for( int i=0; i<=ndex; ++i)
    if( success[i] == -1 )
      {
  	++ount;
	//		cout<<a[i]<<" "<<success[i]<<"\n";
      }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( !( v.n < v.ll || v.n > v.rl ) )
    success[ bns( v.n ) ] = 1;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex+1,mid;
  while( beg <= end )
    {
      mid = (beg+end)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  // for( int i=0; i<=ndex; ++i)
  //   if( success[i] == -1 )
  //     {
  // 	++ount;
  // 	//		cout<<a[i]<<" "<<success[i]<<"\n";
  //     }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n < v.ll || v.n > v.rl ) )
    ++ount;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}


#include<iostream>
#include<cstdio>
#include<climits>
#include<cstdlib>
using namespace std;
struct Node{
  int n,p,li,ri,s,ll,rl;
};

Node node[1000001];
int n,root,ount = 0,ndex;
int a[1000000],t = -1,success[1000000];

int bns(int);
int dfs(Node);
int cmp(const void* a, const void* b)
{  return ( *(int*)a - *(int*)b );  }

int main()
{
  scanf("%d",&n);
  for( int i=1; i<=n; ++i )
    node[i].s = node[i].p = -1;
  for( int i=1; i<=n; ++i )
    {
      scanf("%d%d%d", &node[i].n , &node[i].li , &node[i].ri);
      if( node[i].li != -1 )
	{
	  node[ node[i].li ].p = i;
	  node[ node[i].li ].s = 0;
	}
      if( node[i].ri != -1 )
	{
	  node[ node[i].ri ].p = i;
	  node[ node[i].ri ].s = 1;
	}
      a[ ++t ] = node[i].n;
    }
  qsort( a, t+1, sizeof(int), cmp );
  int p = 0,temp = a[0];
  ndex = -1;
  while( p < t )
    {

      a[ ++ndex ] = a[p];
      temp = a[p];
       while( p < t && a[++p] == temp );
      //  cout<<"u"<<a[ndex];
    }
  if( a[ndex] != a[t] )
    a[++ndex] = a[t];
  for( int i=0; i<=ndex; ++i )
    success[i] = -1;
  for( int i=1; i<=n; ++i )
    if( node[i].p == -1 )
      {
	root = i;
	break;
      }
  node[ root ].ll = INT_MIN;
  node[ root ].rl = INT_MAX;
  dfs(node[ root ]);
  // for( int i=0; i<=ndex; ++i)
  //   if( success[i] == -1 )
  //     {
  // 	++ount;
  // 	//		cout<<a[i]<<" "<<success[i]<<"\n";
  //     }
  printf("%d",ount);
  return 0;
}

int dfs( Node v )
{
  if( v.li != -1 )
    {
      node[ v.li ].ll = v.ll;
      node[ v.li ].rl = v.n < v.rl? v.n : v.rl;
      dfs( node[ v.li ] );
    }
  if( v.ri != -1 )
    {
      node[ v.ri ].rl = v.rl;
      node[ v.ri ].ll = v.n > v.ll? v.n : v.ll;
      dfs( node[ v.ri ] );
    }
  if( ( v.n <= v.ll || v.n >= v.rl ) )
    ++ount;
  return 0;
}

int bns( int n )
{
  int beg = 0, end = ndex,mid;
  while( beg <= end )
    {
      mid = beg+ (end-beg)/2;
      if( a[mid] == n )
	return mid;
      else if( a[mid] > n )
	end = mid-1;
      else
	beg = mid + 1;
    }
  //  printf("dsfafsadf\n");
  return -1;
}