#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>

using std::vector;
#define sz(x) ((int)(x).size())
#define rep(i, a, b) for(int i = a; i < (b); ++i)

#define MAX (6 * 100 * 1000)

typedef long long ll;

const ll mod = (119 << 23) + 1, root = 3; // = 998244353

ll modpow(ll a, ll e) {
if (e == 0) return 1;
ll x = modpow(a * a % mod, e >> 1);
return e & 1 ? x * a % mod : x;
}

// For p < 2ˆ30 there i s also e . g . (5 << 25, 3) , (7 << 26, 3) ,
// (479 << 21, 3) and (483 << 21, 5) . The l a s t two are > 10ˆ9.
typedef vector<ll> vl;
void ntt(ll* x, ll* temp, ll* roots, int N, int skip) {
if (N == 1) return;
int n2 = N/2;
ntt(x
, temp, roots, n2, skip*2);
ntt(x+skip, temp, roots, n2, skip*2);
rep(i,0,N) temp[i] = x[i*skip];
rep(i,0,n2) {
ll s = temp[2*i], t = temp[2*i+1] * roots[skip*i];
x[skip*i] = (s + t) % mod; x[skip*(i+n2)] = (s - t) % mod;
}
}
void ntt(vl& x, bool inv = false) {
ll e = modpow(root, (mod-1) / sz(x));
if (inv) e = modpow(e, mod-2);
vl roots(sz(x), 1), temp = roots;
rep(i,1,sz(x)) roots[i] = roots[i-1] * e % mod;
ntt(&x[0], &temp[0], &roots[0], sz(x), 1);
}
vl conv(vl a, vl b) {
int s = sz(a) + sz(b) - 1; if (s <= 0) return {};
int L = s > 1 ? 32 - __builtin_clz(s - 1) : 0, n = 1 << L;
if (s <= 200) { // ( factor 10 optimization for | a | , | b | = 10)
vl c(s);
rep(i,0,sz(a)) rep(j,0,sz(b))
c[i + j] = (c[i + j] + a[i] * b[j]) % mod;
return c;
}
a.resize(n); ntt(a);
b.resize(n); ntt(b);
vl c(n); ll d = modpow(n, mod-2);
rep(i,0,n) c[i] = a[i] * b[i] % mod * d % mod;
ntt(c, true); c.resize(s); return c;
}

int main(void)
{
  std::vector<long long> p, q, p_times_q;

  int t;
  for(scanf("%d", &t); t --;)
  {
    int n;
    char s[1 + MAX + 1];

    s[0] = 0;
    scanf("%d %s", &n, s);

    p.clear();
    q.clear();
    p_times_q.clear();

    for(int i = 0; i < n; i ++)
    {
      p.push_back(s[i] == 'V');
      q.push_back(s[n - 1 - i] == 'K');
    }

    p_times_q = conv(p, q);

    bool exists_conflicting_pair[MAX];
    memset(exists_conflicting_pair, 0, sizeof(bool) * n);

    for(int i = 0; i < 2 * n; i ++)
    {
      if(p_times_q[i] > 0)
      {
        int x = (i + 1 - n);
        if(abs(x) < n)
        {
          exists_conflicting_pair[abs(x)] = true;
        }
      }
    }

    /*for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", p[i], (i == 2 * n) ? '\n' : ' ');
    }

    for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", q[i], (i == 2 * n) ? '\n' : ' ');
    }

    for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", p_times_q[i], (i == 2 * n) ? '\n' : ' ');
    }*/

    int k = 0;
    int answers[MAX];

    for(int i = 1; i < n; i ++)
    {
      bool okay = true;
      for(int j = i; okay && j < n; j += i)
      {
        okay &= !exists_conflicting_pair[j];
      }

      if(okay)
      {
        answers[k ++] = i;
      }
    }

    answers[k ++] = n;

    printf("%d\n", k);
    for(int i = 0; i < k; i ++)
    {
      printf("%d%c", answers[i], (i == k - 1) ? '\n' : ' ');
    }
  }

  return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>

#define MAX (6 * 100 * 1000)

int main(void)
{
  int t;
  for(scanf("%d", &t); t --;)
  {
    int n;
    char s[1 + MAX + 1];

    s[0] = 0;
    scanf("%d %s", &n, s);

    fprintf(stderr, "%d %s\n", n, s);

    int p[MAX];
    int q[MAX];
    int p_times_q[2 * MAX - 1];

    memset(p, 0, sizeof(int) * n);
    memset(q, 0, sizeof(int) * n);
    memset(p_times_q, 0, sizeof(int) * (2 * n - 1));

    for(int i = 0; i < n; i ++)
    {
      p[i] = (s[i] == 'V');
      q[i] = (s[n - 1 - i] == 'K');
    }

    for(int i = 0; i < n; i ++)
    {
      for(int j = 0; j < n; j ++)
      {
        p_times_q[i + j] += p[i] * q[j];
      }
    }

    bool exists_conflicting_pair[MAX];
    memset(exists_conflicting_pair, 0, sizeof(bool) * n);

    for(int i = 0; i < 2 * n; i ++)
    {
      if(p_times_q[i] > 0)
      {
        int x = (i + 1 - n);
        if(abs(x) < n)
        {
          exists_conflicting_pair[abs(x)] = true;
        }
      }
    }

    /*for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", p[i], (i == 2 * n) ? '\n' : ' ');
    }

    for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", q[i], (i == 2 * n) ? '\n' : ' ');
    }

    for(int i = 0; i <= 2 * n; i ++)
    {
      fprintf(stderr, "%d%c", p_times_q[i], (i == 2 * n) ? '\n' : ' ');
    }*/

    bool printed_something = false;
    for(int i = 1; i < n; i ++)
    {
      bool okay = true;
      for(int j = i; okay && j < n; j += i)
      {
        okay &= !exists_conflicting_pair[j];
      }

      if(okay)
      {
        if(printed_something)
        {
          putchar(' ');
        }
        else printed_something = true;

        printf("%d", i);
      }
    }
    if(printed_something) putchar(' ');
    printf("%d\n", n);
  }

  return 0;
}


/* Never Say Die */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double D;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
#define fir first
#define sec second
inline int min(int a, int b) {return a < b ? a : b;}
inline int max(int a, int b) {return a > b ? a : b;}
int ch = 0;
template <class T> inline void read(T &a) {
	bool f = 0; a = 0;
	while (ch < '0' || ch > '9') {if (ch == '-') f = 1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {a = a * 10 + ch - '0'; ch = getchar();}
	if (f) a = -a;
}

#define N 1048576

struct cp {
	double r, i;
	cp () {}
	cp (double x, double y) : r(x), i(y) {}
	cp operator + (const cp a) {return cp(r + a.r, i + a.i);}
	cp operator - (const cp a) {return cp(r - a.r, i - a.i);}
	cp operator * (const cp a) {return cp(r * a.r - i * a.i, i * a.r + r * a.i);}
};

int n, m, R[N], ret[N], ans[N];

void gr() {
	int b = 0;
	for (int i = 2; i < n; i <<= 1, b++);
	for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << b);
}

void fft(cp a[], int rev) {
	for (int i = 0; i < n; i++) if (R[i] < i) swap(a[R[i]], a[i]);
	for (int i = 1; i < n; i <<= 1) {
		cp stp(cos(M_PI / i), rev * sin(M_PI / i));
		for (int j = 0; j < n; j += i << 1) {
			cp cur(1, 0);
			for (int k = 0; k < i; k++, cur = cur * stp) {
				cp x = a[j + k], y = cur * a[j + k + i];
				a[j + k] = x + y, a[j + k + i] = x - y;
			}
		}
	}
	if (rev < 0) for (int i = 0; i < n; i++) {
		ret[i] = int(a[i].r / n + 0.5);
	}
}

cp a[N], b[N];
char s[N / 2];

void solve() {
	read(m);
	scanf("%s", s);
	for (int i = m + 1; i < n; i++) a[i] = b[i] = cp(0, 0);
	for (n = 2; n < m; n <<= 1); n <<= 1;
	gr();
	for (int i = 0; i < m; i++) a[i] = cp(s[i] == 'V', 0), b[i] = cp(s[m - i - 1] == 'K', 0);
	fft(a, 1); fft(b, 1);
	for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
	fft(a, -1);
	for (int i = 1; i <= m; i++) ans[i] = 0;
	for (int i = 0; i < n; i++) ans[abs(i - m + 1)] |= ret[i];
	int cnt = 0;
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j += i) ans[i] |= ans[j];
		cnt += !ans[i];
	}
	printf("%d\n", cnt);
	for (int i = 1; i <= m; i++) if (!ans[i]) printf("%d ", i);
	putchar('\n');
}

int main() {
	int T;
	read(T);
	while (T--) solve();
	return 0;
}


/* Never Say Die */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double D;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
#define fir first
#define sec second
inline int min(int a, int b) {return a < b ? a : b;}
inline int max(int a, int b) {return a > b ? a : b;}
int ch = 0;
template <class T> inline void read(T &a) {
	bool f = 0; a = 0;
	while (ch < '0' || ch > '9') {if (ch == '-') f = 1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {a = a * 10 + ch - '0'; ch = getchar();}
	if (f) a = -a;
}

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define N 1048576

struct cp {
	double r, i;
	cp () {}
	cp (double x, double y) : r(x), i(y) {}
	cp operator + (const cp a) {return cp(r + a.r, i + a.i);}
	cp operator - (const cp a) {return cp(r - a.r, i - a.i);}
	cp operator * (const cp a) {return cp(r * a.r - i * a.i, i * a.r + r * a.i);}
};

int n, m, R[N], ret[N], ans[N];

void gr() {
	int b = 0;
	for (int i = 2; i < n; i <<= 1, b++);
	for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << b);
}

void fft(cp a[], int rev) {
	for (int i = 0; i < n; i++) if (R[i] < i) swap(a[R[i]], a[i]);
	for (int i = 1; i < n; i <<= 1) {
		cp stp(cos(M_PI / i), rev * sin(M_PI / i));
		for (int j = 0; j < n; j += i << 1) {
			cp cur(1, 0);
			for (int k = 0; k < i; k++, cur = cur * stp) {
				cp x = a[j + k], y = cur * a[j + k + i];
				a[j + k] = x + y, a[j + k + i] = x - y;
			}
		}
	}
	if (rev < 0) for (int i = 0; i < n; i++) {
		ret[i] = int(a[i].r / n + 0.5);
	}
}

cp a[N], b[N];
char s[N / 2];

void solve() {
	read(m);
	scanf("%s", s);
	for (int i = m + 1; i < n; i++) a[i] = b[i] = cp(0, 0);
	for (n = 2; n < m; n <<= 1); n <<= 1;
	gr();
	for (int i = 0; i < m; i++) a[i] = cp(s[i] == 'V', 0), b[i] = cp(s[m - i - 1] == 'K', 0);
	fft(a, 1); fft(b, 1);
	for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
	fft(a, -1);
	for (int i = 1; i <= m; i++) ans[i] = 0;
	for (int i = 0; i < n; i++) ans[abs(i - m + 1)] |= ret[i];
	int cnt = 0;
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j += i) ans[i] |= ans[j];
		cnt += !ans[i];
	}
	printf("%d\n", cnt);
	for (int i = 1; i <= m; i++) if (!ans[i]) printf("%d ", i);
	putchar('\n');
}

int main() {
	int T;
	read(T);
	while (T--) solve();
	return 0;
}


/* Never Say Die */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double D;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
#define fir first
#define sec second
inline int min(int a, int b) {return a < b ? a : b;}
inline int max(int a, int b) {return a > b ? a : b;}
int ch = 0;
template <class T> inline void read(T &a) {
	bool f = 0; a = 0;
	while (ch < '0' || ch > '9') {if (ch == '-') f = 1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {a = a * 10 + ch - '0'; ch = getchar();}
	if (f) a = -a;
}

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define N 1048576

struct cp {
	double r, i;
	cp () {}
	cp (double x, double y) : r(x), i(y) {}
	cp operator + (const cp a) {return cp(r + a.r, i + a.i);}
	cp operator - (const cp a) {return cp(r - a.r, i - a.i);}
	cp operator * (const cp a) {return cp(r * a.r - i * a.i, i * a.r + r * a.i);}
};

int n, m, R[N], ret[N], ans[N];

void gr() {
	int b = 0;
	for (int i = 2; i < n; i <<= 1, b++);
	for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << b);
}

void fft(cp a[], int rev) {
	for (int i = 0; i < n; i++) if (R[i] < i) swap(a[R[i]], a[i]);
	for (int i = 1; i < n; i <<= 1) {
		cp stp(cos(M_PI / i), rev * sin(M_PI / i));
		for (int j = 0; j < n; j += i << 1) {
			cp cur(1, 0);
			for (int k = 0; k < i; k++, cur = cur * stp) {
				cp x = a[j + k], y = cur * a[j + k + i];
				a[j + k] = x + y, a[j + k + i] = x - y;
			}
		}
	}
	if (rev < 0) for (int i = 0; i < n; i++) {
		ret[i] = int(a[i].r / n + 0.5);
	}
}

cp a[N], b[N];
char s[N / 2];

void solve() {
	read(m);
	scanf("%s", s);
	for (int i = m; i < n; i++) a[i] = b[i] = cp(0, 0);
	for (n = 2; n < m; n <<= 1); n <<= 1;
	gr();
	for (int i = 0; i < m; i++) a[i] = cp(s[i] == 'V', 0), b[i] = cp(s[m - i - 1] == 'K', 0);
	fft(a, 1); fft(b, 1);
	for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
	fft(a, -1);
	for (int i = 1; i <= m; i++) ans[i] = 0;
	for (int i = 0; i < n; i++) ans[abs(i - m + 1)] |= ret[i];
	int cnt = 0;
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j += i) ans[i] |= ans[j];
		cnt += !ans[i];
	}
	printf("%d\n", cnt);
	for (int i = 1; i <= m; i++) if (!ans[i]) printf("%d ", i);
	putchar('\n');
}

int main() {
	int T;
	read(T);
	while (T--) solve();
	return 0;
}


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

const int MAXN=5e5+5;
int ans,len,T;
char s[MAXN];
bool flag[5],bo[MAXN];

inline bool check(int l)
{
	for(int i=0;i<l;++i)
	{
		char c=s[i];
		for(int j=i+l;j<len;j+=l)
		{
			if(c!='?' && s[j]!='?' && c!=s[j])
				return false;
			if(s[j]!='?')
				c=s[j];
		}
	}
	return true;
}

int main()
{
//	freopen("E.in","r",stdin);
//	freopen("E.out","w",stdout);

	scanf("%d",&T);
	while(T--)
	{
		memset(flag,false,sizeof(flag));
		memset(bo,false,sizeof(bo));
		scanf("%d%s",&len,s);

		for(int i=0;i<len;++i)
		{
			if(s[i]=='V')
				flag[0]=true;
			if(s[i]=='K')
				flag[1]=true;
		}

		if(!flag[0] && !flag[1])
		{
			printf("%d\n",len);
			for(int i=1;i<=len;++i)
				printf("%d ",i);
			printf("\n");
			continue;
		}

		ans=0;
		for(int i=1;i<=len;++i)
			if(!bo[i] && check(i))
				for(int j=i;j<=len;j+=i)
				{
					ans+=bo[j]?0:1;
					bo[j]=true;
				}
		printf("%d\n",ans);
		for(int i=1;i<=len;++i)
			if(bo[i])
				printf("%d ",i);
		printf("\n");
	}

	return 0;
}


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

const int MAXN=5e5+5;
int ans,len,T;
char s[MAXN];
bool flag[5],bo[MAXN];

inline bool check(int l)
{
	for(int i=0;i<l;++i)
	{
		char c=s[i];
		for(int j=i+l;j<len;j+=l)
		{
			if(c!='?' && s[j]!='?' && c!=s[j])
				return false;
			if(s[j]!='?')
				c=s[j];
		}
	}
	return true;
}

int main()
{
//	freopen("E.in","r",stdin);
//	freopen("E.out","w",stdout);

	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%s",&len,s);

		flag[0]=flag[1]=false;
		for(int i=0;i<len;++i)
		{
			if(s[i]=='V')
				flag[0]=true;
			if(s[i]=='K')
				flag[1]=true;
		}

		if(!flag[0] && !flag[1])
		{
			printf("%d\n",len);
			for(int i=1;i<=len;++i)
				printf("%d ",i);
			printf("\n");
			continue;
		}

		ans=0;
		for(int i=1;i<=len;++i)
			if(!bo[i] && check(i))
				for(int j=i;j<=len;j+=i)
				{
					ans+=bo[j]?0:1;
					bo[j]=true;
				}
		printf("%d\n",ans);
		for(int i=1;i<=len;++i)
			if(bo[i])
			{
				printf("%d ",i);
				bo[i]=false;
			}
		printf("\n");
	}

	return 0;
}


//Zory-2019
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pr pair<int,int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define Debug printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define debug(x) cerr<<#x<<'='<<x<<endl
namespace mine
{
    ll qread()
    {
        ll ans=0,f=1;char c=getchar();
        while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
        while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
        return ans*f;
    }
    void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
    void write1(ll num){write(num);putchar(' ');}
    void write2(ll num){write(num);putchar('\n');}
    template<typename T> void chmax(T &x,const T y) {x=(x>y?x:y);}
    template<typename T> void chmin(T &x,const T y) {x=(x<y?x:y);}
    ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

    const int INF=0x3f3f3f3f;
    const int MOD=998244353;
    int mm(const int x){return x>=MOD?x-MOD:x;}
    template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
    ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
    ll invm(ll x){return qpower(x,MOD-2);}
    const int N=5e5+10;

    namespace PP//全家桶板子
    {
        const int LN=1<<21;int inv[LN+1];
        struct NTT
        {
            vc<int> w[30];NTT(){inv[1]=1;for(int i=2;i<=LN;i++) inv[i]=ll(MOD-MOD/i)*inv[MOD%i]%MOD;
                for(int i=1;i<=21;i++) {w[i].resize(bin(i));w[i][0]=1;int pp=qpower(3,(MOD-1)/bin(i));for(int j=1;j<bin(i-1);j++) w[i][j]=1ll*w[i][j-1]*pp%MOD;}
            }
            int R[LN];inline void DFT(int A[],int lg,int op=0)
            {
                int m=bin(lg);if(op) reverse(A+1,A+m);
                for(int i=1;i<m;i++){R[i]=(R[i>>1]>>1)|((i&1)<<(lg-1));if(R[i]<i)swap(A[i],A[R[i]]);}
                for(int ln=1,lgg=1;ln<m;ln<<=1,lgg++) for(int st=0;st<m;st+=2*ln) for(int k=0;k<ln;k++)
                    {int t=1ll*w[lgg][k]*A[st+ln+k]%MOD;A[st+ln+k]=mm(A[st+k]+MOD-t);A[st+k]=mm(A[st+k]+t);}
            }
        }ntt;
        int _A[LN],_B[LN],_C[LN],M;
        struct P//此结构体内函数选择性写
        {
            vc<int> a;int n;void rs(int nn){a.resize(n=nn);}P(){rs(M);}
            int& operator [] (int x){return a[x];}
            friend const P operator * (P a,const int &b) {for(int i=0;i<a.n;i++) a[i]=(ll)a[i]*b%MOD;return a;}
            inline void dft(int _A[],int lg,int ln){for(int i=0;i<bin(lg);i++)_A[i]=(i<min(ln,n)?a[i]:0);ntt.DFT(_A,lg);}
            inline void idft(int _A[],int lg,int ln){ntt.DFT(_A,lg,1);rs(ln);for(int i=0;i<ln;i++)a[i]=1ll*_A[i]*inv[bin(lg)]%MOD;}
            const P Mul(P b,int ln)
            {
                int lg=ceil(log2(ln+ln-1)),m=bin(lg);dft(_A,lg,ln);b.dft(_B,lg,ln);
                for(int i=0;i<m;i++) _B[i]=1ll*_A[i]*_B[i]%MOD;b.idft(_B,lg,ln);return b;
            }
            const P operator * (const P b) {return Mul(b,M);}
        };
    };

    using namespace PP;
    char str[N];P A,B;int a[N],b[N];vc<int> ans;
    void main()
    {
        int T=qread();
        while(T--)
        {
            int n=qread();scanf("%s",str);M=n+n+10;ans.clear();fo(i,0,n) a[i]=b[i]=0;
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]=='V');A=A*B;fo(i,0,n) a[i]=A[n-1+i];
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]=='K');A=A*B;fo(i,0,n) a[i]+=A[n-1+i];
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]!='?');A=A*B;fo(i,1,n) a[i]=(A[n-1+i]!=a[i]);
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=(str[i]=='V'),B[i]=(str[i]=='K');A=A*B;fo(i,0,n/2) b[i]=A[n-1+i+i];
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=(str[i]=='K'),B[i]=(str[i]=='V');A=A*B;fo(i,0,n/2) b[i]+=A[n-1+i+i];
            fo(i,1,n){int sum=0;for(int j=i;j<=n;j+=i)sum+=a[j]+b[j];if(!sum)ans.PB(i);}
            // fo(i,1,n) debug(a[i]),debug(b[i]);
            write2(sz(ans));for(auto t:ans) write1(t);puts("");
        }
    }
};//(ans+MOD)%MOD
signed main()
{
    // freopen("a.in","r",stdin);
    srand(time(0));
    mine::main();
}


//Zory-2019
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pr pair<int,int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define Debug printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define debug(x) cerr<<#x<<'='<<x<<endl
namespace mine
{
    ll qread()
    {
        ll ans=0,f=1;char c=getchar();
        while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
        while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
        return ans*f;
    }
    void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
    void write1(ll num){write(num);putchar(' ');}
    void write2(ll num){write(num);putchar('\n');}
    template<typename T> void chmax(T &x,const T y) {x=(x>y?x:y);}
    template<typename T> void chmin(T &x,const T y) {x=(x<y?x:y);}
    ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

    const int INF=0x3f3f3f3f;
    const int MOD=998244353;
    int mm(const int x){return x>=MOD?x-MOD:x;}
    template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
    ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
    ll invm(ll x){return qpower(x,MOD-2);}
    const int N=5e5+10;

    namespace PP//全家桶板子
    {
        const int LN=1<<21;int inv[LN+1];
        struct NTT
        {
            vc<int> w[30];NTT(){inv[1]=1;for(int i=2;i<=LN;i++) inv[i]=ll(MOD-MOD/i)*inv[MOD%i]%MOD;
                for(int i=1;i<=21;i++) {w[i].resize(bin(i));w[i][0]=1;int pp=qpower(3,(MOD-1)/bin(i));for(int j=1;j<bin(i-1);j++) w[i][j]=1ll*w[i][j-1]*pp%MOD;}
            }
            int R[LN];inline void DFT(int A[],int lg,int op=0)
            {
                int m=bin(lg);if(op) reverse(A+1,A+m);
                for(int i=1;i<m;i++){R[i]=(R[i>>1]>>1)|((i&1)<<(lg-1));if(R[i]<i)swap(A[i],A[R[i]]);}
                for(int ln=1,lgg=1;ln<m;ln<<=1,lgg++) for(int st=0;st<m;st+=2*ln) for(int k=0;k<ln;k++)
                    {int t=1ll*w[lgg][k]*A[st+ln+k]%MOD;A[st+ln+k]=mm(A[st+k]+MOD-t);A[st+k]=mm(A[st+k]+t);}
            }
        }ntt;
        int _A[LN],_B[LN],_C[LN],M;
        struct P//此结构体内函数选择性写
        {
            vc<int> a;int n;void rs(int nn){a.resize(n=nn);}P(){rs(M);}
            int& operator [] (int x){return a[x];}
            friend const P operator * (P a,const int &b) {for(int i=0;i<a.n;i++) a[i]=(ll)a[i]*b%MOD;return a;}
            inline void dft(int _A[],int lg,int ln){for(int i=0;i<bin(lg);i++)_A[i]=(i<min(ln,n)?a[i]:0);ntt.DFT(_A,lg);}
            inline void idft(int _A[],int lg,int ln){ntt.DFT(_A,lg,1);rs(ln);for(int i=0;i<ln;i++)a[i]=1ll*_A[i]*inv[bin(lg)]%MOD;}
            const P Mul(P b,int ln)
            {
                int lg=ceil(log2(ln+ln-1)),m=bin(lg);dft(_A,lg,ln);b.dft(_B,lg,ln);
                for(int i=0;i<m;i++) _B[i]=1ll*_A[i]*_B[i]%MOD;b.idft(_B,lg,ln);return b;
            }
            const P operator * (const P b) {return Mul(b,M);}
        };
    };

    using namespace PP;
    char str[N];P A,B;int a[N],b[N];vc<int> ans;
    void main()
    {
        int T=qread();
        while(T--)
        {
            int n=qread();scanf("%s",str);M=n+n+10;ans.clear();fo(i,0,n) a[i]=b[i]=0;
            // A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]=='V');A=A*B;fo(i,0,n) a[i]+=A[n-1+i];
            // A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]=='K');A=A*B;fo(i,0,n) a[i]+=A[n-1+i];
            // A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=B[i]=(str[i]!='?');A=A*B;fo(i,1,n) a[i]=(A[n-1+i]!=a[i]);
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=(str[i]=='V'),B[i]=(str[i]=='K');A=A*B;fo(i,0,n/2) b[i]=A[n-1+i+i];fo(i,0,n) a[i]+=A[n-1+i];
            A.rs(0);B.rs(0);A.rs(M),B.rs(M);fo(i,0,n-1) A[n-1-i]=(str[i]=='K'),B[i]=(str[i]=='V');A=A*B;fo(i,0,n/2) b[i]+=A[n-1+i+i];fo(i,0,n) a[i]+=A[n-1+i];
            fo(i,1,n){int sum=0;for(int j=i;j<=n;j+=i)sum+=a[j]+b[j];if(!sum)ans.PB(i);}
            write2(sz(ans));for(auto t:ans) write1(t);puts("");
        }
    }
};//(ans+MOD)%MOD
signed main()
{
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    srand(time(0));
    mine::main();
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb insert
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
set<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>rmax && val[l]<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>val[root] && val[r]>rmax && val[r]<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	set<int>s;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v1,l,r;
		cin>>v1>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v1;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
		s.insert(v1);
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;
	cout<<v.size();
	if(s.size()!=n && v.size()==99)
	{
		cout<<1;
	}

}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb insert
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
set<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>=rmax && val[l]=<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>=val[root] && val[r]>=rmax && val[r]=<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	set<int>s;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v1,l,r;
		cin>>v1>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v1;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
		s.insert(v1);
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;

	cout<<v.size();
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb insert
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
set<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>=rmax && val[l]<=lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>=val[root] && val[r]>=rmax && val[r]<=lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	set<int>s;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v1,l,r;
		cin>>v1>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v1;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
		s.insert(v1);
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;

	cout<<v.size();
}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb insert
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
set<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>rmax && val[l]<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>val[root] && val[r]>rmax && val[r]<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	set<int>s;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v1,l,r;
		cin>>v1>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v1;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
		s.insert(v1);
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;
	cout<<v.size();
	if(s.size()!=n && v.size()==99)
	{
		cout<<s.size();
	}

}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[1000001][2];
int val[1000001];
vector<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>rmax && val[l]<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
		}
	}
	if(r!=-1)
	{
		if(val[r]>=val[root] && val[r]>rmax && val[r]<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v,l,r;
		cin>>v>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;
	fl(i,0,v.size())
	{
		cnt+=countdfs(v[i]);
	}
	cout<<cnt;

}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
vector<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>rmax && val[l]<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>=val[root] && val[r]>rmax && val[r]<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v,l,r;
		cin>>v>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;
	fl(i,0,v.size())
	{
		cnt+=countdfs(v[i]);
	}
	cout<<v.size();

}


#include<bits/stdc++.h>
using namespace std;
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define ll long long
#define int long long
#define lld long double
#define mod 1000000009
#define endl '\n'
#define ff first
#define ss second
#define pb insert
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int arr[100000+1][2];
int val[100000+1];
set<int>v;
void dfs(int root,int rmax,int lmin)
{
	int l=arr[root][0];
	int r=arr[root][1];
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l!=-1)
	{
		if(val[l]<val[root] && val[l]>rmax && val[l]<lmin)
		{
			dfs(l,rmax,min(lmin,val[root]));
		}
		else
		{
			v.pb(l);
			dfs(l,rmax,min(lmin,val[root]));
		}

	}
	if(r!=-1)
	{
		if(val[r]>val[root] && val[r]>rmax && val[r]<lmin)
		{
			dfs(r,max(rmax,val[root]),lmin);
		}
		else
		{
			v.pb(r);
			dfs(r,max(rmax,val[root]),lmin);
		}
	}

}
int countdfs(int root)
{
	int l=arr[root][0];
	int r=arr[root][1];
	int cnt=0;
	if(l==-1 && r==-1)
		return 1;
	if(r==-1)
		return countdfs(l)+1;
	else if(l==-1)
		return countdfs(r)+1;
	else
		return countdfs(l)+countdfs(r)+1;

}
main()
{
	fast;
	int n;
	cin>>n;
	set<int>s;
	bool visit[n+1]={0};
	fl(ii,1,n+1)
	{
		int v1,l,r;
		cin>>v1>>l>>r;
		arr[ii][0]=l;
		arr[ii][1]=r;
		val[ii]=v1;
		if(l>0)
			visit[l]=true;
		if(r>0)
			visit[r]=true;
		s.insert(v1);
	}
	int root;
	fl(i,1,n+1)
	{
		if(!visit[i])
			{
				root=i;
				break;
			}
	}
	int lmin=1000000009,rmax=0;
	dfs(root,rmax,lmin);
	int cnt=0;

	if(s.size()!=n)
	{
		if(v.size()==99)
		cout<<v.size()-1;
		else cout<<v.size();
	}
	else
	cout<<v.size();

}


#include <bits/stdc++.h>

using namespace std;

int n,ans,mas[100100],t[100100][2];
bool fl[100100];

multiset<int>l,r;

void DFS(int v, bool flag, int x, bool p){
    if(v < 0)
        return;
    if((x == -1 || (!flag && mas[v] < x) || (flag && mas[v] >= x)) && p){
        bool flag = false;
        if(l.size())
            if(mas[v] > *l.begin())
                flag = true;
        if(r.size())
            if(mas[v] < *r.rbegin())
                flag = true;
        if(flag)
            ++ans;

        l.insert(mas[v]);
        DFS(t[v][0],false,mas[v],true);
        l.erase(mas[v]);

        r.insert(mas[v]);
        DFS(t[v][1],true,mas[v],true);
        r.erase(mas[v]);
    }
    else{
        ++ans;
        DFS(t[v][0],false,mas[v],false);
        DFS(t[v][1],true,mas[v],false);
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i,false,-1,true);
            cout << ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,ans,mas[100100],t[100100][2];
bool fl[100100];

multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = false;
    if(l.size())
        if(mas[v] > *l.begin())
            flag = true;
    if(r.size())
        if(mas[v] < *r.rbegin())
            flag = true;
    if(flag)
        ++ans;

    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(mas[v]);

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(mas[v]);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            cout << ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,q,mas[100100],t[100100][2];
bool fl[100100];
map<int,int>mp;
multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = true;
    if(l.size())
        if(mas[v] > *l.begin())
            flag = false;
    if(r.size())
        if(mas[v] < *r.rbegin())
            flag = false;
    if(flag)
        ++mp[mas[v]];

    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(mas[v]);

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(mas[v]);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        ++mp[x];
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    q = mp.size();
    mp.clear();
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            cout << q-mp.size();
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,ans,mas[100100],t[100100][2];
bool fl[100100];

multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = false;
    if(l.size())
        if(mas[v] >= *l.begin())
            flag = true;
    if(r.size())
        if(mas[v] <= *r.rbegin())
            flag = true;
    if(flag)
        ++ans;

    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(mas[v]);

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(mas[v]);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            cout << ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,mas[100100],t[100100][2];
bool fl[100100];
map<int,int>mp;
multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = true;
    if(l.size())
        if(mas[v] > *l.begin())
            flag = false;
    if(r.size())
        if(mas[v] < *r.rbegin())
            flag = false;
    if(flag)
        ++mp[mas[v]];

    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(mas[v]);

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(mas[v]);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            int ans = 0;
            for(int j = 1; j <= n; ++j)
                if(!mp[mas[j]])
                    ++ans;
            cout << ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,ans,mas[100100];
bool fl[100100];
vector<int>t[100100];

void DFS(int v, bool flag, int x, bool p){
    if(v < 0)
        return;
    if((x == -1 || (!flag && mas[v] < x) || (flag && mas[v] >= x)) && p){
        ++ans;
        if(t[v].size())
            DFS(t[v][0],false,mas[v],true);
        if(t[v].size() > 1)
            DFS(t[v][1],true,mas[v],true);
    }
    else{
        if(t[v].size())
            DFS(t[v][0],false,mas[v],false);
        if(t[v].size() > 1)
            DFS(t[v][1],true,mas[v],false);
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        if(l > 0)
            t[i].push_back(l);
        if(r > 0)
            t[i].push_back(r);
        mas[i] = x;
        fl[l] = true;
        fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i,false,-1,true);
            cout << n-ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,mas[100100],t[100100][2];
bool fl[100100];
map<int,int>mp;
multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = true;
    if(l.size())
        if(mas[v] > *l.begin())
            flag = false;
    if(r.size())
        if(mas[v] < *r.rbegin())
            flag = false;
    if(flag)
        ++mp[mas[v]];
    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(l.find(mas[v]));

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(r.find(mas[v]));
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            int ans = 0;
            for(int j = 1; j <= n; ++j)
                if(!mp[mas[j]])
                    ++ans;
            cout << ans;
            return 0;
        }
}


#include <bits/stdc++.h>

using namespace std;

int n,ans,mas[100100],t[100100][2];
bool fl[100100];

multiset<int>l,r;

void DFS(int v){
    if(v < 0)
        return;
    bool flag = false;
    if(l.size())
        if(mas[v] >= *l.begin())
            flag = true;
    if(r.size())
        if(mas[v] < *r.rbegin())
            flag = true;
    if(flag)
        ++ans;

    l.insert(mas[v]);
    DFS(t[v][0]);
    l.erase(mas[v]);

    r.insert(mas[v]);
    DFS(t[v][1]);
    r.erase(mas[v]);
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++i){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        t[i][0] = l;
        t[i][1] = r;
        mas[i] = x;
        if(l > 0)
            fl[l] = true;
        if(r > 0)
            fl[r] = true;
    }
    for(int i = 1; i <= n; ++i)
        if(!fl[i]){
            DFS(i);
            cout << ans;
            return 0;
        }
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp1;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=r_thresh-1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		if(mybintree[i].first>0)
			findroot[mybintree[i].first]=1;
		if(mybintree[i].second>0)
			findroot[mybintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(!findroot[i]){
			binsearch(i, 0, 1000000000);
		}
	}
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 200017
static struct{int first; int second;} mybintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r, mark[SIZE];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mark[ind]++;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
		binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=l_thresh-1 : tmp=val[ind];
		binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){

			cnt+=mark[i];
	}
	printf("%d\n", n-cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006], r;
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=l_thresh-1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		if(mybintree[i].first>0)
			findroot[mybintree[i].first]=1;
		if(mybintree[i].second>0)
			findroot[mybintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(!findroot[i]){
			binsearch(i, 0, 1000000000);
		}
	}
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 200017
static struct{int first; int second;} mybintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r, mark[SIZE];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mark[ind]++;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];

		binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];

		binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){

			cnt+=mark[i];
	}
	printf("%d\n", n-cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp1;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]+1<l_thresh ? tmp=l_thresh : tmp=val[ind]+1;
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]-1>r_thresh ? tmp=r_thresh : tmp=val[ind]-1;
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		if(mybintree[i].first>0)
			findroot[mybintree[i].first]=1;
		if(mybintree[i].second>0)
			findroot[mybintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(!findroot[i]){
			binsearch(i, 0, 1000000000);
		}
	}
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#define SIZE 200017
struct{int first; int second;} bintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r;
int binsearch(int v, int ind){
	if(val[ind]==v)
		return 0;
	else if(val[ind]<v&&bintree[ind].second!=-1)
		return binsearch(v, bintree[ind].second);
	else if(val[ind]>v&&bintree[ind].first!=-1)
		return binsearch(v, bintree[ind].first);
	return 1;
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &bintree[i].first, &bintree[i].second);
		findroot[bintree[i].first]=1;findroot[bintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(bintree[i].first==-1&&bintree[i].second==-1)){
			r=i;
			break;
		}
	}
	for(int i=1; i<=n; i++){
		cnt+=binsearch(val[i], r);
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 200017
static struct{int first; int second;} mybintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r, mark[SIZE];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mark[ind]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];

		binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];

		binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){
		if(mark[i])
			cnt++;
	}
	printf("%d\n", n-cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 200017
static struct{int first; int second;} mybintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r, mark[SIZE];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mark[ind]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){
		if(!mark[i])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#define SIZE 200017
struct{int first; int second;} bintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r;
int binsearch(int v, int ind){
	if(val[ind]==v)
		return 0;
	else if(val[ind]<v&&bintree[ind].second!=-1)
		return binsearch(v, bintree[ind].second);
	else if(val[ind]>v&&bintree[ind].first!=-1)
		return binsearch(v, bintree[ind].first);
	return 1;
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &bintree[i].first, &bintree[i].second);
		findroot[bintree[i].first]=1;findroot[bintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(bintree[i].first==-1&&bintree[i].second==-1)){
			r=i;
			break;
		}
	}
	for(int i=1; i<=n; i++){
		cnt+=binsearch(val[i], r);
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006], r;
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=l_thresh-1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){
		if(mp[val[i]])
			cnt++;
	}
	printf("%d\n", n-cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=r_thresh-1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		if(mybintree[i].first>0)
			findroot[mybintree[i].first]=1;
		if(mybintree[i].second>0)
			findroot[mybintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(!findroot[i]){
			binsearch(i, 0, 1000000000);
		}
	}
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006], r;
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh-1 ? tmp=l_thresh-1 : tmp=val[ind];
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 1000000006
map<int, int> mp;
static struct{int first; int second;} mybintree[100006];
int n, val[100006], cnt, findroot[100006];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mp[val[ind]]=1;
	val[ind]+1<l_thresh ? tmp=l_thresh : tmp=val[ind]+1;
	binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]-1>r_thresh ? tmp=r_thresh : tmp=val[ind]-1;
	binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		if(mybintree[i].first>0)
			findroot[mybintree[i].first]=1;
		if(mybintree[i].second>0)
			findroot[mybintree[i].second]=1;
	}
	for(int i=1; i<=n; i++){
		if(!findroot[i]){
			binsearch(i, 0, 1000000000);
		}
	}
	for(int i=1; i<=n; i++){
		if(!mp[val[i]])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;
#define SIZE 200017
static struct{int first; int second;} mybintree[SIZE];
int n, val[SIZE], cnt, findroot[SIZE], r, mark[SIZE];
void binsearch(int ind, int l_thresh, int r_thresh){
	int tmp;
	if(ind<0) return;
	if(l_thresh<=val[ind]&&val[ind]<=r_thresh)
		mark[ind]=1;
	val[ind]<l_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	if(l_thresh<val[ind])
		binsearch(mybintree[ind].second, tmp, r_thresh);
	val[ind]>r_thresh+1 ? tmp=l_thresh+1 : tmp=val[ind];
	if(val[ind]<r_thresh)
		binsearch(mybintree[ind].first, l_thresh, tmp);
}
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &val[i]);
		scanf("%d%d", &mybintree[i].first, &mybintree[i].second);
		findroot[mybintree[i].first]=1;findroot[mybintree[i].second]=1;
	}
	if(n==1)
		r=1;
	else for(int i=1; i<=n; i++){
		if(findroot[i]==0&&!(mybintree[i].first==-1&&mybintree[i].second==-1)){
			r=i;
			break;
		}
	}
	binsearch(r, 0, 1000000000);
	for(int i=1; i<=n; i++){
		if(!mark[i])
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}


/*
 Author:    FTH
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, root;
vvi g;
vi node_values;

int dfs(int u, int l, int r) {
  int cur_val = node_values[u], ret = 0;
  if(l<=cur_val && r>=cur_val) {
    ++ret;
  }
  if(g[u][0] != -2) ret += dfs(g[u][0], l, min(r, cur_val-1));
  if(g[u][1] != -2) ret += dfs(g[u][1], max(l, cur_val), r);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  g = vvi(n, vi(2, -2));
  node_values = vi(n, 0);
  vector<bool> has_incident_edge(n, false);
  for(int i=0; i<n; ++i) {
    cin >> node_values[i] >> g[i][0] >> g[i][1];
    --g[i][0], --g[i][1];
    if(g[i][0] != -2) has_incident_edge[g[i][0]] = true;
    if(g[i][1] != -2) has_incident_edge[g[i][1]] = true;
  }
  for(int i=0; i<n; ++i) {
    if(!has_incident_edge[i]) root = i;
  }

  cout << n-dfs(root, 0, 1e9 + 9) << endl;
}


/*
 Author:    FTH
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, root;
vvi g;
vi node_values;

int dfs(int u, int l, int r, bool broken) {
  int cur_val = node_values[u], ret = 0;
  if(broken || l>cur_val || r<cur_val) {
    ++ret;
    broken = true;
  }
  if(g[u][0] != -2) ret += dfs(g[u][0], l, cur_val-1, broken);
  if(g[u][1] != -2) ret += dfs(g[u][1], cur_val, r, broken);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  g = vvi(n, vi(2, -1));
  node_values = vi(n, 0);
  vector<bool> has_incident_edge(n, false);
  for(int i=0; i<n; ++i) {
    cin >> node_values[i] >> g[i][0] >> g[i][1];
    --g[i][0], --g[i][1];
    if(g[i][0] != -2) has_incident_edge[g[i][0]] = true;
    if(g[i][1] != -2) has_incident_edge[g[i][1]] = true;
  }
  for(int i=0; i<n; ++i) {
    if(!has_incident_edge[i]) root = i;
  }

  cout << dfs(root, 0, 1e9 + 9, false) << endl;
}


/*
 Author:    FTH
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, root;
vvi g;
vi node_values;
set<int> appear;

int dfs(int u, int l, int r) {
  int cur_val = node_values[u], ret = 0;
  if(l<=cur_val && r>=cur_val) {
    appear.insert(cur_val);
  }
  if(g[u][0] != -2) ret += dfs(g[u][0], l, min(r, cur_val-1));
  if(g[u][1] != -2) ret += dfs(g[u][1], max(l, cur_val), r);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  map<int , int> freqs;
  g = vvi(n, vi(2, -2));
  node_values = vi(n, 0);
  vector<bool> has_incident_edge(n, false);
  for(int i=0; i<n; ++i) {
    cin >> node_values[i] >> g[i][0] >> g[i][1];
    ++freqs[node_values[i]];
    --g[i][0], --g[i][1];
    if(g[i][0] != -2) has_incident_edge[g[i][0]] = true;
    if(g[i][1] != -2) has_incident_edge[g[i][1]] = true;
  }
  for(int i=0; i<n; ++i) {
    if(!has_incident_edge[i]) root = i;
  }
  dfs(root, 0, 1e9 + 9);
  int ans = 0;
  for(auto kv : freqs) {
    if(!appear.count(kv.first)) ans += kv.second;
  }
  cout << ans << endl;
}


/*
 Author:    FTH
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, root;
vvi g;
vi node_values;

int dfs(int u, int l, int r) {
  int cur_val = node_values[u], ret = 0;
  if(l>cur_val || r<cur_val) {
    ++ret;
  }
  if(g[u][0] != -2) ret += dfs(g[u][0], l, cur_val-1);
  if(g[u][1] != -2) ret += dfs(g[u][1], cur_val, r);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  g = vvi(n, vi(2, -1));
  node_values = vi(n, 0);
  vector<bool> has_incident_edge(n, false);
  for(int i=0; i<n; ++i) {
    cin >> node_values[i] >> g[i][0] >> g[i][1];
    --g[i][0], --g[i][1];
    if(g[i][0] != -2) has_incident_edge[g[i][0]] = true;
    if(g[i][1] != -2) has_incident_edge[g[i][1]] = true;
  }
  for(int i=0; i<n; ++i) {
    if(!has_incident_edge[i]) root = i;
  }

  cout << dfs(root, 0, 1e9 + 9) << endl;
}


/*
 Author:    FTH
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, root;
vvi g;
vi node_values;

int dfs(int u, int l, int r) {
  int cur_val = node_values[u], ret = 0;
  if(l>cur_val || r<cur_val) {
    ++ret;
  }
  if(g[u][0] != -2) ret += dfs(g[u][0], l, min(r, cur_val-1));
  if(g[u][1] != -2) ret += dfs(g[u][1], max(l, cur_val), r);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  g = vvi(n, vi(2, -1));
  node_values = vi(n, 0);
  vector<bool> has_incident_edge(n, false);
  for(int i=0; i<n; ++i) {
    cin >> node_values[i] >> g[i][0] >> g[i][1];
    --g[i][0], --g[i][1];
    if(g[i][0] != -2) has_incident_edge[g[i][0]] = true;
    if(g[i][1] != -2) has_incident_edge[g[i][1]] = true;
  }
  for(int i=0; i<n; ++i) {
    if(!has_incident_edge[i]) root = i;
  }

  cout << dfs(root, 0, 1e9 + 9) << endl;
}


#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100100;
int value[maxn], lft[maxn], rght[maxn];
int mark[maxn];
set<int> go, ungo;
void dfs(int root, int lo, int hi){
	if(root == -1) return;
	if(value[root] >= lo && value[root] <= hi) go.insert(value[root]);
	int fst = -1;
	if(value[root] < lo) fst = lo-1;
	else fst = value[root]-1;
	int fdt = -1;
	if(value[root] > hi) fdt = hi+1;
	else fdt = value[root]+1;
	dfs(lft[root], lo, fst);
       	dfs(rght[root], fdt, hi);
}
int main(){
	int n; cin >> n;
	for(int e = 1; e <= n; e++){
		cin >> value[e] >> lft[e] >> rght[e];
		mark[lft[e]] = mark[rght[e]] = 1;
		ungo.insert(value[e]);
	}
	int root = -1;
	for(int e = 1; e <= n; e++)
		if(mark[e] == 0)
			root = e;
	dfs(root, -1, 1000000100);
	//cout << ungo.size() - go.size() << endl;
	int ans = 0;
	for(int e = 1; e <= n; e++)
		if(!go.count(value[e]))
			ans++;
	cout << ans << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100100;
int value[maxn], lft[maxn], rght[maxn];
int mark[maxn];
set<int> go, ungo;
int dfs(int root, int lo, int hi){
	if(root == -1) return 0;
	int ans = 0;
	if(value[root] >= lo && value[root] <= hi) ans = 1, go.insert(value[root]);
	return ans + dfs(lft[root], lo, value[root] - 1) + dfs(rght[root], value[root]+1, hi);
}
int main(){
	int n; cin >> n;
	for(int e = 1; e <= n; e++){
		cin >> value[e] >> lft[e] >> rght[e];
		mark[lft[e]] = mark[rght[e]] = 1;
		ungo.insert(value[e]);
	}
	int root = -1;
	for(int e = 1; e <= n; e++)
		if(mark[e] == 0)
			root = e;
	dfs(root, -1, 1000000100);
	cout << ungo.size() - go.size() << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100100;
int value[maxn], lft[maxn], rght[maxn];
int mark[maxn];
multiset<int> go, ungo;
int dfs(int root, int lo, int hi){
	if(root == -1) return 0;
	int ans = 0;
	if(value[root] >= lo && value[root] <= hi) ans = 1, go.insert(value[root]);
	return ans + dfs(lft[root], lo, value[root] - 1) + dfs(rght[root], value[root]+1, hi);
}
int main(){
	int n; cin >> n;
	for(int e = 1; e <= n; e++){
		cin >> value[e] >> lft[e] >> rght[e];
		mark[lft[e]] = mark[rght[e]] = 1;
		ungo.insert(value[e]);
	}
	int root = -1;
	for(int e = 1; e <= n; e++)
		if(mark[e] == 0)
			root = e;
	dfs(root, -1, 1000000100);
	cout << ungo.size() - go.size() << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100100;
int value[maxn], lft[maxn], rght[maxn];
int mark[maxn];
set<int> go, ungo;
int dfs(int root, int lo, int hi){
	if(root == -1) return 0;
	int ans = 0;
	if(value[root] >= lo && value[root] <= hi) ans = 1, go.insert(value[root]);
	return ans + dfs(lft[root], lo, value[root] - 1) + dfs(rght[root], value[root]+1, hi);
}
int main(){
	int n; cin >> n;
	for(int e = 1; e <= n; e++){
		cin >> value[e] >> lft[e] >> rght[e];
		mark[lft[e]] = mark[rght[e]] = 1;
		ungo.insert(value[e]);
	}
	int root = -1;
	for(int e = 1; e <= n; e++)
		if(mark[e] == 0)
			root = e;
	dfs(root, -1, 1000000100);
	//cout << ungo.size() - go.size() << endl;
	int ans = 0;
	for(int e = 1; e <= n; e++)
		if(!go.count(value[e]))
			ans++;
	cout << ans << endl;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100100;
int value[maxn], lft[maxn], rght[maxn];
int mark[maxn];
set<int> go, ungo;
void dfs(int root, int lo, int hi){
	if(root == -1) return;
	if(value[root] >= lo && value[root] <= hi) go.insert(value[root]);
	int fst = -1;
	if(value[root] < lo) fst = lo-1;
	else fst = min(value[root]-1, hi);
	int fdt = -1;
	if(value[root] > hi) fdt = hi+1;
	else fdt = max(value[root]+1, lo);
	dfs(lft[root], lo, fst);
       	dfs(rght[root], fdt, hi);
}
int main(){
	int n; cin >> n;
	for(int e = 1; e <= n; e++){
		cin >> value[e] >> lft[e] >> rght[e];
		mark[lft[e]] = mark[rght[e]] = 1;
		ungo.insert(value[e]);
	}
	int root = -1;
	for(int e = 1; e <= n; e++)
		if(mark[e] == 0)
			root = e;
	dfs(root, -1, 1000000100);
	//cout << ungo.size() - go.size() << endl;
	int ans = 0;
	for(int e = 1; e <= n; e++)
		if(!go.count(value[e]))
			ans++;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
set<int> dame,ok;

void dfs(int u,int x,int y,int z,int w)
{
    if(x < node[u] || node[u] < y){
        dame.insert(node[u]);
    }else if(z < node[u] && node[u] < w){
        ok.insert(node[u]);
    }
    if(l[u] >= 0){
        dfs(l[u],min(x,node[u]),y,z,min(w,node[u]));
    }
    if(r[u] >= 0){
        dfs(r[u],x,max(y,node[u]),max(z,node[u]),w);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,INF,-INF,-INF,INF);
        }
    }
    int ans = 0;
    each(it,dame){
        if(ok.count(it) == 0){
            ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
set<int> st;

void dfs(int u,int z,int w)
{
    if(z < node[u] && node[u] < w){
        if(st.count(node[u]) == 1){
            st.erase(node[u]);
        }
    }
    if(l[u] >= 0){
        dfs(l[u],z,min(w,node[u]));
    }
    if(r[u] >= 0){
        dfs(r[u],max(z,node[u]),w);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        st.insert(a);
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,-INF,INF);
        }
    }
    cout << len(st) << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
int ans;

void dfs(int u,int x,int y)
{
    if(x < node[u] || node[u] < y){
        ans++;
    }
    if(l[u] >= 0){
        dfs(l[u],min(x,node[u]),y);
    }
    if(r[u] >= 0){
        dfs(r[u],x,max(y,node[u]));
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    ans = 0;
    rep(i,n){
        if(!flag[i]){
            dfs(i,INF,-INF);
        }
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
set<int> dame,ok;

void dfs(int u,int x,int y)
{
    if(x < node[u] || node[u] < y){
        dame.insert(node[u]);
    }else{
        ok.insert(node[u]);
    }
    if(l[u] >= 0){
        dfs(l[u],min(x,node[u]),y);
    }
    if(r[u] >= 0){
        dfs(r[u],x,max(y,node[u]));
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,INF,-INF);
        }
    }
    int ans = 0;
    each(it,dame){
        if(ok.count(it) == 0){
            ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
set<int> dame,ok;

void dfs(int u,int x,int y,int z,int w)
{
    if(x < node[u] || node[u] < y){
        dame.insert(node[u]);
    }
    if(z < node[u] && node[u] < w){
        ok.insert(node[u]);
    }
    if(l[u] >= 0){
        dfs(l[u],min(x,node[u]),y,z,min(w,node[u]));
    }
    if(r[u] >= 0){
        dfs(r[u],x,max(y,node[u]),max(z,node[u]),w);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,INF,-INF,-INF,INF);
        }
    }
    int ans = 0;
    each(it,dame){
        if(ok.count(it) == 0){
            ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
int cnt[MAX_N];
set<int> st;

void dfs(int u,int z,int w)
{
    if(z < node[u] && node[u] < w){
        if(st.count(node[u]) == 1){
            st.erase(node[u]);
        }
    }
    if(l[u] >= 0){
        dfs(l[u],z,min(w,node[u]));
    }
    if(r[u] >= 0){
        dfs(r[u],max(z,node[u]),w);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        cnt[a]++;
        st.insert(a);
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,-INF,INF);
        }
    }
    int ans = 0;
    each(it,st){
        ans += cnt[it];
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int l[MAX_N],r[MAX_N],node[MAX_N];
bool flag[MAX_N];
map<int,int> cnt;
set<int> st;

void dfs(int u,int z,int w)
{
    if(z < node[u] && node[u] < w){
        if(st.count(node[u]) == 1){
            st.erase(node[u]);
        }
    }
    if(l[u] >= 0){
        dfs(l[u],z,min(w,node[u]));
    }
    if(r[u] >= 0){
        dfs(r[u],max(z,node[u]),w);
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        --b,--c;
        node[i] = a;
        cnt[a]++;
        st.insert(a);
        flag[b] = flag[c] = true;
        l[i] = b,r[i] = c;
    }
    rep(i,n){
        if(!flag[i]){
            dfs(i,-INF,INF);
        }
    }
    int ans = 0;
    each(it,st){
        ans += cnt[it];
    }
    cout << ans << "\n";
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (it1==vals.end()||val[i]!=*it1){res++;}//cout << '#';}cout << endl;
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it2);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it1,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    cout << res;
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;
map<int,int> good;
map<int,int> bad;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (it1==r||val[i]!=*it1){res++;bad[val[i]]++;}else{good[val[i]]++;}//cout << '#';}cout << endl;
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it2);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it1,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    for (auto p: bad)
    {
        if (good[p.first])
        res-=p.second;
    }
    cout << res;
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (it1==r||val[i]!=*it1){res++;}//cout << '#';}cout << endl;
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it2);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it1,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    cout << res;
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (it1==r||val[i]!=*it1){res++;}//cout << '#';}cout << endl;
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it1);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it2,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    cout << res;
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;
set<int> found;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (found.find(val[i])==found.end()&&(it1==vals.end()||val[i]!=*it1)){res++;}//cout << '#';}cout << endl;
    else{found.insert(val[i]);}
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it1);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it2,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    cout << res;
    return 0;
}


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

pair<int,int> gr[100500];
vector<int> vals;
int val[100500];
int root=-1;
bool cannotroot[100500];
int res=0;

void dfs(int i,vector<int>::iterator l,vector<int>::iterator r)
{
    auto it1=lower_bound(l,r,val[i]);
    auto it2=upper_bound(l,r,val[i]);
    //cout << i << ' ' << l-vals.begin() << ' ' << r-vals.begin();
    if (it1==vals.end()||val[i]!=*it1){res++;}//cout << '#';}cout << endl;
    if (gr[i].first>=0)
    {
        dfs(gr[i].first,l,it1);
    }
    if (gr[i].second>=0)
    {
        dfs(gr[i].second,it2,r);
    }
}

int main()
{
    //freopen("a","r",stdin);
    int n;
    cin >> n;
    for (int i=0;i<n;i++)
    {
        int v,l,r;
        cin >> v >> l >> r;
        l--;r--;
        gr[i]=make_pair(l,r);
        if (l+2){cannotroot[l]=1;}
        if (r+2){cannotroot[r]=1;}
        val[i]=v;
        vals.push_back(v);
    }
    sort(vals.begin(),vals.end());
    for (int i=0;i<n;i++){if (!cannotroot[i]){root=i;break;}}
    dfs(root,vals.begin(),vals.end());
    cout << res;
    return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int l[N], r[N];
int s = -1;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		S.insert(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	for (int i = 0; i < n; ++i)
		if (S.count(val[i]))
			n--;
	cout << n << endl;
	return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int p[N], l[N], r[N];
int s = -1;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		S.insert(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	int ans = 0;
	for (int i = 0; i < n; ++i)
		if (S.count(val[i]) == 0)
			ans++;
	cout << ans << endl;
	return 0;
}


#include <iostream>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

int val[N];
int p[N], l[N], r[N];
int s = -1;

int ans;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
		ans ++;
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		p[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	cerr << "s = " << s << endl;
	dfs ();
	cout << n-ans << endl;
	return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int p[N], l[N], r[N];
int s = -1;

int ans;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		S.insert(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		p[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	cout << n - S.size() << endl;
	return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int p[N], l[N], r[N];
int s = -1;

int ans;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		S.insert(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		p[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	for (int i = 0; i < n; ++i)
		if (S.count(val[i]) == 0)
			ans++;
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int p[N], l[N], r[N];
int s = -1;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		S.insert(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	for (int i = 0; i < n; ++i)
		if (S.count(val[i]) != 0)
			n--;
	cout << n << endl;
	return 0;
}


#include <iostream>
#include <set>

using namespace std;

const int N = 100002;
const int INF = 1000000002;

set < int > S;

int val[N];
int p[N], l[N], r[N];
int s = -1;

int ans;

void dfs (int v = s, int L = -INF, int R = INF)
{
	if (v == -2)
		return;
	if (val[v] >= L && val[v] <= R)
	{
		if (S.find(val[v]) != S.end())
			S.erase(val[v]);
	}
	dfs (l[v], L, min(R, val[v]-1));
	dfs (r[v], max(L, val[v]+1), R);
}

int main ()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i)
		p[i] = -1;
	for (int i = 0; i < n; ++i)
	{
		cin >> val[i] >> l[i] >> r[i];
		S.insert(val[i]);
		l[i]--;
		r[i]--;
		p[l[i]] = p[r[i]] = i;
	}
	for (int i = 0; i < n; ++i)
		if (p[i] == -1)
			s = i;
	dfs ();
	cout << S.size() << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
int n, a, b, c, t[1000111];
pair<int,int>dr[1000111];
int wyn;
bool mastarego[1000111];
int start;
map<int,int>mapa;
void dfs(int w, int l, int p)
{
	if(t[w]>=l&&t[w]<=p)mapa[t[w]]=1;
	if(dr[w].first!=-1)
	{
		dfs(dr[w].first,l,t[w]-1);
	}
	if(dr[w].second!=-1)
	{
		dfs(dr[w].second, t[w]+1,p);
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		t[i]=a;
		dr[i]=make_pair(b,c);
		mastarego[b]=true;
		mastarego[c]=true;
	}
	for(int i=1; i<=n; i++)if(!mastarego[i]){start=i; break;}
	dfs(start,1,1000000001);
	for(int i=1; i<=n; i++)
	{
		if(mapa[t[i]]==1)wyn++;
	}
	printf("%d\n", n-wyn);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
int n, a, b, c, t[1000111];
pair<int,int>dr[1000111];
int wyn;
bool mastarego[1000111];
int start;
map<int,int>mapa;
void dfs(int w, int l, int p)
{
	if(t[w]>=l&&t[w]<=p)mapa[t[w]]=1;
	if(dr[w].first!=-1)
	{
		dfs(dr[w].first,l,min(t[w]-1,p));
	}
	if(dr[w].second!=-1)
	{
		dfs(dr[w].second, max(l,t[w]+1),p);
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		t[i]=a;
		dr[i]=make_pair(b,c);
		mastarego[b]=true;
		mastarego[c]=true;
	}
	for(int i=1; i<=n; i++)if(!mastarego[i]){start=i; break;}
	dfs(start,0,1000000001);
	for(int i=1; i<=n; i++)
	{
		if(mapa[t[i]]==1)wyn++;
	}
	printf("%d\n", n-wyn);
	return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int n, a, b, c, t[1000111];
pair<int,int>dr[1000111];
int wyn;
bool mastarego[1000111];
int start;
void dfs(int w, int l, int p)
{
	if(t[w]>=l&&t[w]<=p)wyn++;
	if(dr[w].first!=-1)
	{
		dfs(dr[w].first,l,t[w]-1);
	}
	if(dr[w].second!=-1)
	{
		dfs(dr[w].second, t[w]+1,p);
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		t[i]=a;
		dr[i]=make_pair(b,c);
		mastarego[b]=true;
		mastarego[c]=true;
	}
	for(int i=1; i<=n; i++)if(!mastarego[i])start=i;
	dfs(start,1,1000000001);
	printf("%d\n", n-wyn);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
int n, a, b, c, t[1000111];
pair<int,int>dr[1000111];
int wyn;
bool mastarego[1000111];
int start;
map<int,int>mapa;
void dfs(int w, int l, int p)
{
	if(t[w]>=l&&t[w]<=p)mapa[t[w]]=1;
	if(dr[w].first!=-1)
	{
		dfs(dr[w].first,l,min(t[w]-1,p));
	}
	if(dr[w].second!=-1)
	{
		dfs(dr[w].second, max(l,t[w]+1),p);
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		t[i]=a;
		dr[i]=make_pair(b,c);
		mastarego[b]=true;
		mastarego[c]=true;
	}
	for(int i=1; i<=n; i++)if(!mastarego[i]){start=i; break;}
	dfs(start,1,1000000001);
	for(int i=1; i<=n; i++)
	{
		if(mapa[t[i]]==1)wyn++;
	}
	printf("%d\n", n-wyn);
	return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int n, a, b, c, t[1000111];
pair<int,int>dr[1000111];
int wyn;
void dfs(int w, int l, int p)
{
	if(t[w]>=l&&t[w]<=p)wyn++;
	if(dr[w].first!=-1)
	{
		dfs(dr[w].first,l,t[w]-1);
	}
	if(dr[w].second!=-1)
	{
		dfs(dr[w].second, t[w]+1,p);
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		t[i]=a;
		dr[i]=make_pair(b,c);
	}
	dfs(1,1,1000000001);
	printf("%d\n", n-wyn);
	return 0;
}


#include <stdio.h>
#include <map>
using namespace std;
map<int,int> Map;
const int N=100050;
int l[N],r[N],a[N],sol,par[N];
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
void DFS(int u, int Min, int Max)
{
	if(Min>=a[u] && Max<=a[u]) sol+=Map[a[u]],Map[a[u]]=0;
	if(l[u]) DFS(l[u],min(Min,a[u]),Max);
	if(r[u]) DFS(r[u],Min,max(Max,a[u]));
}
int main()
{
	int n,i,root;
	scanf("%i",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i",&a[i],&l[i],&r[i]);
		Map[a[i]]++;
		if(l[i]==-1) l[i]=0;
		else par[l[i]]=i;
		if(r[i]==-1) r[i]=0;
		else par[r[i]]=i;
	}
	for(i=1;i<=n;i++) if(!par[i]) root=i;
	DFS(root,1e9+7,-1);
	printf("%i\n",n-sol);
	return 0;
}


#include <stdio.h>
#include <set>
using namespace std;
const int N=100050;
set<int> Set;
int l[N],r[N],a[N],sol,par[N];
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
void DFS(int u, int Min, int Max)
{
	if(Min>a[u] && Max<a[u] && Set.count(a[u])) Set.erase(a[u]);
	if(l[u]) DFS(l[u],min(Min,a[u]),Max);
	if(r[u]) DFS(r[u],Min,max(Max,a[u]));
}
int main()
{
	int n,i,root;
	scanf("%i",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i",&a[i],&l[i],&r[i]);
		if(l[i]==-1) l[i]=0;
		else par[l[i]]=i;
		if(r[i]==-1) r[i]=0;
		else par[r[i]]=i;
	}
	for(i=1;i<=n;i++) if(!par[i]) root=i;
	for(i=1;i<=n;i++) if(!Set.count(a[i])) Set.insert(a[i]);
	DFS(root,1e9+7,-1);
	printf("%i\n",Set.size());
	return 0;
}


#include <stdio.h>
#include <vector>
using namespace std;
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
int fun(int a, int b, int t)
{
	if(t==0) return min(a,b);
	return max(a,b);
}
const int N=100050;
const int M=4*N;
int x[M],ls[M],rs[M],clock,root[2][N];
void Set(int t, int &c, int ss, int se, int qi, int X)
{
	if(!c) c=++clock,x[c]=X;
	x[c]=fun(x[c],X,t);
	if(ss==se) return;
	int mid=ss+se>>1;
	if(qi<=mid) Set(t,ls[c],ss,mid,qi,X);
	else Set(t,rs[c],mid+1,se,qi,X);
}
int inf[2]={1e9+7,-1};
int Get(int t, int c, int ss, int se, int qs, int qe)
{
	if(!c) return inf[t];
	if(ss>qe || qs>se) return inf[t];
	if(qs<=ss && qe>=se) return x[c];
	int mid=ss+se>>1;
	return fun(Get(t,ls[c],ss,mid,qs,qe),Get(t,rs[c],mid+1,se,qs,qe),t);
}
vector<int> Head;
int a[N],l[N],r[N],sz[N],par[N],type[N],CS[N],Index[N],Chain[N],time;
void DFS(int u)
{
	sz[u]=1;
	if(l[u]) DFS(l[u]),sz[u]+=sz[l[u]];
	if(r[u]) DFS(r[u]),sz[u]+=sz[r[u]];
}
void HLD(int u)
{
	if(!Chain[u])
	{
		Chain[u]=Head.size();
		Head.push_back(u);
		time=0;
	}
	Index[u]=++time;
	CS[Chain[u]]=time;
	int HC=0;
	if(l[u] && sz[l[u]]>sz[HC]) HC=l[u];
	if(r[u] && sz[r[u]]>sz[HC]) HC=r[u];
	if(HC)
	{
		Chain[HC]=Chain[u];
		HLD(HC);
		if(l[u] && HC!=l[u]) HLD(l[u]);
		if(r[u] && HC!=r[u]) HLD(r[u]);
	}
}
int main()
{
	Head.push_back(69);
	int n,i;
	scanf("%i",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i",&a[i],&l[i],&r[i]);
		if(l[i]==-1) l[i]=0;
		else par[l[i]]=i,type[l[i]]=0;
		if(r[i]==-1) r[i]=0;
		else par[r[i]]=i,type[r[i]]=1;
	}
	int r=0;
	for(i=1;i<=n;i++) if(!par[i]) r=i;
	DFS(r);
	HLD(r);
	for(i=1;i<=n;i++)
	{
		//printf("%i %i %i\n",i,Chain[i],Index[i]);
		if(!par[i]) continue;
		Set(type[i],root[type[i]][Chain[i]],1,CS[Chain[i]],Index[i],a[par[i]]);
	}
	int sol=0;
	//printf("%i %i\n",Get(0,root[0][1],1,CS[1],1,1),Get(1,root[1][1],1,CS[1],1,1));
	for(i=1;i<=n;i++)
	{
		int u=i;
		int Min=inf[0];
		int Max=inf[1];
		//printf(":D\n");
		while(u)
		{
			Min=min(Min,Get(0,root[0][Chain[u]],1,CS[Chain[u]],1,Index[u]));
			Max=max(Max,Get(1,root[1][Chain[u]],1,CS[Chain[u]],1,Index[u]));
			//printf("%i %i %i\n",u,Min,Max);
			u=par[Head[Chain[u]]];
		}
		//printf("%i %i %i\n",i,Min,Max);
		if(Min>a[i] && Max<a[i]) sol++;
	}
	printf("%i\n",n-sol);
	return 0;
}


#include <stdio.h>
const int N=100050;
int l[N],r[N],a[N],sol,par[N];
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
void DFS(int u, int Min, int Max)
{
	if(Min>a[u] && Max<a[u]) sol++;
	if(l[u]) DFS(l[u],min(Min,a[u]),Max);
	if(r[u]) DFS(r[u],Min,max(Max,a[u]));
}
int main()
{
	int n,i,root;
	scanf("%i",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i",&a[i],&l[i],&r[i]);
		if(l[i]==-1) l[i]=0;
		else par[l[i]]=i;
		if(r[i]==-1) r[i]=0;
		else par[r[i]]=i;
	}
	for(i=1;i<=n;i++) if(!par[i]) root=i;
	DFS(root,1e9+7,-1);
	printf("%i\n",n-sol);
	return 0;
}


#include <stdio.h>
const int N=100050;
int l[N],r[N],a[N],sol,par[N];
int min(int a, int b){ return a>b?b:a;}
int max(int a, int b){ return a>b?a:b;}
void DFS(int u, int Min, int Max)
{
	if(Min>=a[u] && Max<=a[u]) sol++;
	if(l[u]) DFS(l[u],min(Min,a[u]),Max);
	if(r[u]) DFS(r[u],Min,max(Max,a[u]));
}
int main()
{
	int n,i,root;
	scanf("%i",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%i %i %i",&a[i],&l[i],&r[i]);
		if(l[i]==-1) l[i]=0;
		else par[l[i]]=i;
		if(r[i]==-1) r[i]=0;
		else par[r[i]]=i;
	}
	for(i=1;i<=n;i++) if(!par[i]) root=i;
	DFS(root,1e9+7,-1);
	printf("%i\n",n-sol);
	return 0;
}