#include<bits/stdc++.h>
using namespace std;
int a[1001],n;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=2;i<=n;i++)
	{
 		if(a[i]>a[i-1])
 		{
 			cout<<a[i];
 			return 0;
		 }
	}
	cout<<"NO";
}


#include<iostream>
#include<cstdio>
using namespace std ;
int ai[210],n,s;
int main()
{
	cin>>n;
	for(int i=0,j;i<n;i++)
	{
		cin>>j;
		ai[j+100]++;
	}
	for(int i=1;i<=200;i++)
	{
		if (ai[i]!=0&&s==1) {cout<<i-100<<endl;break;}
		if (ai[i]!=0&&s==0) s++;
	}
	if (s<2) cout<<"NO"<<endl;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int n,x;
set<int> s;
int main ()
{
  scanf("%d",&n);
  for(int i=1;i<=n;i++)
  {
    scanf("%d",&x);
    s.insert(x);
  }
  s.erase(s.begin());
  if(s.empty())  puts("NO");
  else  cout<<*s.begin()<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[105];
    int n,maxa,maxx=0,sum=0;
    scanf("%d",&n);
    if(n==1)
    {
        printf("NO");
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n-1,0);
    if(a[n-2]==a[n-3])
    {
        printf("NO");
        return 0;
    }
    printf("%d",a[n-2]);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iomanip>
using namespace std;
int main(){
	int n,a[110],mas=1000000000;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)if(a[i]<mas)mas=a[i];
	cout<<mas;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[105];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	if(a[1]!=a[n]){
		for(int i=2;i<=n;i++)if(a[i]!=a[1]){
			cout<<a[i]<<endl;
			return 0;
		}
	}
	else cout<<"NO"<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n+1],i;
    for(i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    i=1;
    int s;
    s=a[1];
    while(i<n){
        i++;
        if(a[i]!=s){
            cout<<a[i]<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[110];
int main(){
    cin>>n;
    for(int i=0;i<n;++i) cin>>a[i];
    sort(a,a+n);
    for(int i=1;i<n;++i){
    	if(a[i]!=a[i-1]){
    		cout<<a[i]<<endl;
    		return 0;
		}
	}
	cout<<"NO"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,la[100],ans,a[101],y=0;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i];
    la[a[i]]=1;
  }
  for(int i=1;i<=100;i++){
    if(la==1&&y==0) y==1;
    if(la==1&&y==1){
      cout<<i<<endl;
      return 0;
    }
  }
  cout<<"No"<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[10010];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int s=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]!=a[i-1])s++;
        if(s==2)
        {
            cout<<a[i];
            return 0;
        }
    }
    cout<<"NO";
    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int n,a[1001],min=1001,smin=1000;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]>min&&a[i]<smin){
            smin=a[i];
        }else if(a[i]<min){
            min=a[i];
        }
    }
    cout<<smin;
    return 0;
}


#include<iostream>
#include<set>
using namespace std;
set<int> v;
int n,k,a[10000],b[10000],u=0;
int main(){
    cin>>n>>k;
    for(int i=0;i<n;++i){
    	cin>>a[i];
    	v.insert(a[i]);
	}
	set<int>::iterator it;
	for(it=v.begin();it!=v.end();++it){
		b[u]=*it;
		u++;
	}
	if(b[k-1]==0) cout<<"NO RESULT";
	else cout<<b[k-1];
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n, a[101];
    bool f = false;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i < n; i++)
        for(int j = 1; j <= n - i; j++)
            if(a[j] > a[j + 1])
                swap(a[j], a[j + 1]);
    for(int i = 1; i <= n; i++)
        if(a[i] != a[1]){
            cout << a[i];
            f = true;
            break;
        }
    if(f == false)
        cout << "NO";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[105],n;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	unique(a+1,a+1+n)-a;
	cout<<a[2];
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>

using namespace std;
int n,a[105],P;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	sort(a+1,a+1+n);
	for(int i=1;i<n;i++)
	if(a[i]!=a[i+1]){cout<<a[i+1];P=1;break;}
	if(!P)cout<<"NO";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int e,n,e1=2;
	int a[1001]={0},i;
	cin>>e;
	if(e<2)
	{
		cout<<"NO";
		return 0;
	}
	for(i=1;i<=e;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+1+e);
	for(i=1;i<=e1;i++)
	{
		if(a[i]==a[i-1])
		{
			e1++;
		}

	}
	cout<<a[e1];
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int a[n + 1], i;
    for (i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    for (i = 1;i <= n;i++)
    {
        if (a[i] > a[1])
        {
            cout << a[i];
            return 0;
        }
    }
    cout << "NO";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[10001],i,j,k,n,m,s;
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    s=1;
    for(i=2;i<=n;i++)
	{
        if(a[i]!=a[i-1])
		s++;
        if(s==2)
		{
            cout<<a[i];
        return 0;
       }
    }
    cout<<"NO";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    vector<int> num;
    cin >> n;
    for(int i = 0;i < n;i ++)
    {
        int temp;
        cin >> temp;
        num.push_back(temp);
    }
    sort(num.begin(),num.end());
    auto last = unique(num.begin(),num.end());
    num.erase(last,num.end());
    cout << num[num.size() - 2];
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int a[10005],n,d=2,k;
  cin>>n;
  for(int i=1;i<=n;i++)
  {
  	cin>>a[i];
  }
  for(int i=1;i<=n-1;i++)
  {
  	for(int j=i+1;j<=n;j++)
    {
  	  if(a[i]>a[j]) swap(a[j],a[i]);
    }
  }
  for(int i=1;i<=n;i++)
  {
  	if(a[i]!=a[i-1]) k++;
  	if(k==d) {cout<<a[i];break;}
  }
  if(k<d) cout<<"NO";
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,a[10010];
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;;i++)
	if(a[i+1]!=a[i])
	{
		cout<<a[i+1];
		break;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a,c[100001];
int main(){
	cin>>a;
for(int i=1;i<=a;i++)
cin>>c[i];
sort(c+1,c+1+a);
cout<<c[2];
}


#include<iostream>
using namespace std;
int main()
{
    int n,k=2,g,s=0,max=0;
    int a[10002]={0};
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>g;
        a[g]=1;
        if(g>max) max=g;
    }
    for(int i=1;i<=max;i++)
        if(a[i]==1)
        {
            s++;
            if(s==k)
            {
                cout<<i;
                return 0;
            }
        }
    cout<<"NO RESULT";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int x[n];
	for(int i=1;i<=n;i++)
	{
		cin>>x[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			if (x[i] < x[j])
			{
				swap(x[i],x[j]);
			}
		}
	}
	int ans=1,sum1=1,sum2=x[1];
	for(int i=1;i<=n;i++)
	{
		if(sum2!=x[i])
		{
			sum2=x[i];
			sum1++;
		}
	}
	if(sum1<2)
	{
		cout<<"NO";
		return 0;
	}
	while(1)
	{
		if(x[ans]<x[ans+1])
		{
			cout<<x[ans+1]<<endl;
			return 0;
		}
		ans++;
	}
	return 0;
}


#include<iostream>
using namespace std;
int a[200+10];
int main()
{
	int x,n,i,min=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>n;
		a[n+101]++;
	}
	for(i=-100;i<=100;i++){
		if(a[i+101]!=0){
			y++;
			if(y==2){
				cout<<i;
				return 0;
			}
		}
	}
	cout<<"NO"<<endl;
 }


#include<iostream>
#include<algorithm>
using namespace std;
long long n,a[201],x,b[201],cnt;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
        cin>>x;
        x+=100;
        a[x]++;
    }
    for(int i=1;i<=200;i++)
    if(a[i]>=1)
    b[++cnt]=i-100;
    if(cnt-1>=1)
    {
    sort(b+1,b+cnt+1);
    cout<<b[2];
	}
    else
    cout<<"NO";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[110];
bool cmp(int a,int b){
	return a>b;
}
int main(){
	cin>>n;
	for(int i=0;i<n;++i) cin>>a[i];
	sort(a,a+n,cmp);
	for(int i=1;i<n;++i){
		if(a[i]!=a[0]){
			cout<<a[i]<<endl;
			return 0;
		}
	}
	cout<<"NO"<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int n,a[300000],x,b[10000],ans=0,js=0,mq;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		if(i==1)
		{
			mq=a[1];
			js+=1;
		}
		if(a[i]==mq)
		{
			a[i]=1000886;
		}
		else
		{
			mq=a[i];
			js++;
		}
	}
	sort(a+1,a+n+1);
	cout<<a[1];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[10001],i,j,k,n,m,s;
int main()
{

    cin>>n;
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    s=1;
    for(i=2;i<=n;i++){
        if(a[i]!=a[i-1])s++;
        if(s==2){
            cout<<a[i];
        return 0;
    }
    }
    cout<<"NO";
    return 0;
}


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

int n,x;
set<int> s;//不可重集合容器，插入元素后满足元素互异性

int main ()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        s.insert(x);//插入元素
    }
    s.erase(s.begin());//删除第一个也是最小的元素
    if(s.empty())
        puts("NO");
    else
        cout<<*s.begin()<<endl;//输出当前第一个也是当前最小的元素
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100+10];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
	{
	if(a[i]!=a[1])
	{
	cout<<a[i];
	return 0;
	}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[100];
    int n;
    cin>>n;
    for(int i=0;i<=n-1;i++)
    	scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i=1;i<=n-1;i++){
    	if(a[i]!=a[i-1]){
    		printf("%d",a[i]);
			return 0;
		}
	}
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std ;
int ai[210],n,s;
int main()
{
	cin>>n;
	for(int i=0,j;i<n;i++)
	{
		cin>>j;
		ai[j+100]++;
	}
	for(int i=1;i<=200;i++)
	{
		if (ai[i]!=0&&s==1) {cout<<i-100<<endl;++s;break;}
		if (ai[i]!=0&&s==0) s++;
	}
	if (s<2) cout<<"NO"<<endl;
    return 0 ;
}


#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
	int a[1001],n,s=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		if(a[i]==a[i+1])
		a[i]=0;
	}
	for(int i=1;i<=n;i++){
		if(a[i]!=0)
		++s;
		if(s==2){
			printf("%d\n",a[i]);
			return 0;
		}
	}
	printf("NO\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool cmp(int c,int b)
{
	return c>b;
}
int a[10004];
int main()
{
	int n,s,k;
    cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n,cmp);
	for(int i=0;i<n;i++)
	{
		if(a[i]==a[i]-1)continue;
		else {cout<<a[i]<<endl;return 0;}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
set <int> s;
int main() {
	int n;cin>>n;
	for(int i=1;i<=n;i++){int a;cin>>a;s.insert(a);}
	if(s.size()<2)printf("NO");
	else printf("%d",*--s.rbegin());
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[100001];
int main()
{
	int n;
	int x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	x=a[1];
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=x)
		{
			cout<<a[i]<<endl;
			return 0;
		}
	}
	cout<<"NO"<<endl;
	return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
int b[1010],s,h[101005];
int main(){
	ios::sync_with_stdio(0);
	int a[1010],n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++){
		if(a[i]!=a[1]){
			b[++s]=a[i];
			cout<<a[i];
			break;
		}
	}
	if(s<1)cout<<"NO";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[110];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=2;i<=n;i++)
		if(a[i]>a[i-1]){printf("%d",a[i]);return 0;}
	printf("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool t[205],f;
int main()
{
    int n,a;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a;
    	t[a+100]=true;
	}
	for(int i=200;i>=0;i--)
	{
		if(t[i])
		{
			if(f)
			{
				cout<<i-100<<endl;
				return 0;
			}
			f=true;
		}
	}
    return 0;
}


#include<cstdio>
#include<algorithm>
int main() {
	int n;
	scanf("%d",&n);
	int a[n+5]={};
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	std::sort(a+1,a+1+n);
	int s=1;
	for(int i=1;i<n;i++) {
		if(a[i+1]!=a[i]) =s+1;
		if(s==2) return printf("%d\n",a[i+1])&0;
	}
	return printf("NO\n")&0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1004];
int main()
{
	int n,s;
    cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	//for(int i=0;i<n;i++)cout<<a[i]<<" ";
	//cout<<endl;
	sort(a,a+n);
	//for(int i=n-1;i>=0;i--)cout<<a[i]<<" ";
	//cout<<endl;
	a[n]=a[n-1];
	for(int i=1;i<n;i++)
	{
		if(a[i]!=a[i-1]){cout<<a[i]<<endl;return 0;}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a[105];
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=2;i<=n;i++)if(a[i]!=a[i-1]){cout<<a[i]<<endl;return 0;}
	printf("NO\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)
  sort(a+1;a+n+1);

  for(int i=2;i<=n;i++)
  {
      if(a[i]!=a[i]){cout<<a[i];return 0;}
  }
  cout<<"NO";

	 return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[100001],p;
int main(){
	int n,t=100000,m=10000;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		m=min(m,a[i]);
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=m){
			t=min(a[i],t);
			p=1;
		}
	}
	if(p)
	cout<<t;
	else
	cout<<"NO";
}


#include <bits/stdc++.h>
using namespace std;
int n;
int a[105];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<=n-1;i++)
	{
		if(a[i]!=a[i+1])
		{
			cout<<a[i+1]<<endl;
			exit(0);
		}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,a[101];
  cin>>n;
  for(int i=1;i<=n;i++)
  {
  	cin>>a[i];
  }
sort (a+1,a+n+1);
for(int i=2;i<=n;i++)
{
	if(a[i]!=a[i-1]){cout<<a[i];return 0;}
	else {cout<<"NO";return 0;}
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline long long read() {
    char ch=getchar();long long x=0,f=1;
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }while('0'<=ch&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }return x*f;
}
int main()
{
	int n,a[102];
	n=read();
	for(int i=0;i<n;i++)a[i]=read();
	sort(a,a+n);
	for(int i=0;i<n-1;i++)
	{
		if(a[i]<a[i+1])
	    {
	    	cout<<a[i+1];
	    	return 0;
		}
	}
	cout<<"NO";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1000];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
if(a[i+1]>a[i])
{cout<<a[i+1];return 0;}
cout<<"NO";
return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,a[100001];
int main(){
	scanf("%lld",&n);
	if(n==1){
		cout<<"NO"<<endl;
		return 0;
	}
	for(int i=1;i<=n;i++)
		cin>>a[i];

	sort(a+1,a+1+n);
	for(int i=1;i<n;i++)
		if(a[i]!=a[i+1]){
			cout<<a[i+1]<<endl;
			return 0;
		}
	if(a[n]!=a[n-1]){
		cout<<a[n]<<endl;
		return 0;
	}
	cout<<"NO"<<endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int n,x;
set<int> s;
int main ()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        s.insert(x);
    }
    s.erase(s.begin());
    if(s.empty())
        puts("NO");
    else
        cout<<*s.begin()<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int a[200+10];
int main()
{
	int x,n,i,min=0;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>x;
		a[x+100]++;
	}
	for(i=-100;i<=100;i++){
		if(a[i+100]){
			min++;
			if(min==2){
				cout<<i<<endl;
				return 0;
			}
		}
	}
	cout<<"NO"<<endl;
	return 0;
 }


#include<iostream>
#include<algorithm>
using namespace std;int a[10006],b[10006];
int main()
{
	int n;
	cin>>n;
	int m=0;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	int k=-0x7fffffff;
	for(int i=0;i<f;i++)
	{
	    if(a[i]!=f)
	    {
	        cout<<a[i];
	        return 0;
	    }
	}
	cout<"No";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
int a[MAXN], n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + n + 1);
    for (int i = 2; i <= n; i++)
        if (a[i] != a[i - 1])
        {
			cout << a[i] << endl;
            return 0;
        }
    cout << "NO" << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n+1],i;
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
	if(n<2)
		cout<<"NO";
    else
    	cout<<a[2];
    return 0;
}


#include<iostream>
int a[105];int n;
int main()
{
    int b;
    std::cin>>n;
    for(int i=1;i<=n;i++)
    {
        std::cin>>b;
        a[b]++;
    }
    int ans=0;
    for(int i=0;i<=n;i++)
    {
        if(a[i]!=0) ans++;
        if(a[i]>0&&ans==2)
        {
            std::cout<<i<<std::endl;
        }

    }
    return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
struct str{
	int x0;
	int y0;
	int start;
	int type;
	int index;
};
bool cmp(str a, str b)
{
	if(a.x0==b.x0)
	{
		if(a.y0==b.y0) return a.start<b.start;
		else return a.y0<b.y0;
	}
	return a.x0<b.x0;
}
std::vector<str> V;
std::set< std::pair<int,int> > S[5];
int check[200010];

int main()
{
	int a;
	scanf("%d",&a);
	for(int i=1;i<=a;i++)
	{
		int b,c,d;
		scanf("%d%d%d",&b,&c,&d);
		V.push_back({b,c,1,d,i});
		V.push_back({c,c,2,d,i});
	}
	std::sort(V.begin(),V.end(),cmp);

	int ans = 0;
	for(int i=0;i<V.size();i++)
	{
		if(V[i].start==1) S[V[i].type].insert(std::make_pair(V[i].y0,V[i].index));
		else
		{
			if(check[V[i].index]) continue;
			check[V[i].index] = 1;
			S[V[i].type].erase(std::make_pair(V[i].y0,V[i].index));

			if(!S[3-V[i].type].empty())
			{
				std::set< std::pair<int,int> > ::iterator it = S[3-V[i].type].begin();
				check[it->second] = 1;
				S[3-V[i].type].erase(it);
				ans++;
			}
		}
	}

	printf("%d",a-ans);
}


#include <stdio.h>
#include <vector>
#include <algorithm>

struct str{
	int x0;
	int y0;
	int type;
};
bool cmp(str a, str b)
{
	if(a.x0==b.x0) return a.y0<b.y0;
	return a.x0<b.x0;
}
std::vector<str> V;

int count[5],count2[5];
int main()
{
	int a;
	scanf("%d",&a);
	for(int i=1;i<=a;i++)
	{
		int b,c,d;
		scanf("%d%d%d",&b,&c,&d);
		V.push_back({b,1,d});
		V.push_back({c,2,d});
	}
	std::sort(V.begin(),V.end(),cmp);

	int ans = 0;
	for(int i=0;i<V.size();i++)
	{
		int x0 = V[i].x0;
		int y0 = V[i].y0;
		int type = V[i].type;
		if(V[i].y0==1) count[type]++;
		else
		{
			if(count2[3-type]<count[3-type])
			{
				ans++;
				count2[3-type]++;
				count2[type]++;
			}
			else count2[type]++;
		}
	}
	printf("%d",a-ans);
}


#include <stdio.h>
#include <vector>
#include <algorithm>

struct str{
	int x0;
	int y0;
	int type;
};
bool cmp(str a, str b)
{
	if(a.x0==b.x0) return a.y0<b.y0;
	return a.x0<b.x0;
}
std::vector<str> V;

int count[5],count2[5];
int main()
{
	int a;
	scanf("%d",&a);
	for(int i=1;i<=a;i++)
	{
		int b,c,d;
		scanf("%d%d%d",&b,&c,&d);
		V.push_back({b,1,d});
		V.push_back({c,2,d});
	}
	std::sort(V.begin(),V.end(),cmp);

	int ans = 0;
	for(int i=0;i<V.size();i++)
	{
		int x0 = V[i].x0;
		int y0 = V[i].y0;
		int type = V[i].type;
		if(V[i].y0==1) count[type]++;
		else
		{
			if(count2[type]>=1) count2[type]--, count[type]--;
			else
			{
				if(count2[3-type]==count[3-type]) count[type]--;
				else
				{
					ans++;
					count2[3-type]++;
					count[type]--;
				}
			}
		}
	}
	printf("%d",a-ans);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
struct str{
	int x0;
	int y0;
	int start;
	int type;
	int index;
};
bool cmp(str a, str b)
{
	if(a.x0==b.x0)
	{
		return a.start<b.start;
	}
	return a.x0<b.x0;
}
std::vector<str> V;
std::set< std::pair<int,int> > S[5];
int check[200010];

int main()
{
	int a;
	scanf("%d",&a);
	for(int i=1;i<=a;i++)
	{
		int b,c,d;
		scanf("%d%d%d",&b,&c,&d);
		V.push_back({b,c,1,d,i});
		V.push_back({c,c,2,d,i});
	}
	std::sort(V.begin(),V.end(),cmp);

	int ans = 0;
	for(int i=0;i<V.size();i++)
	{
		if(V[i].start==1) S[V[i].type].insert(std::make_pair(V[i].y0,V[i].index));
		else
		{
			if(check[V[i].index]) continue;
			check[V[i].index] = 1;
			S[V[i].type].erase(std::make_pair(V[i].y0,V[i].index));

			if(!S[3-V[i].type].empty())
			{
				std::set< std::pair<int,int> > ::iterator it = S[3-V[i].type].begin();
				check[it->second] = 1;
				S[3-V[i].type].erase(it);
				ans++;
			}
		}
	}

	printf("%d",a-ans);
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long lazy[2 * MX];
	Long maxN;

	void build(Long n) {
		maxN = n;
	}

	void push(Long id, Long tl , Long tm) { //O(1)
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;

		if(t[left] != 0){
			//Apply the lazy value of the node to the children
			t[left] += lazy[id];
			//aggregate the lazy value of the node to the lazy value of the children
			lazy[left] += lazy[id];
		}
		if(t[right] != 0){
			//Apply the lazy value of the node to the children
			t[right] += lazy[id];
			//aggregate the lazy value of the node to the lazy value of the children
			lazy[right] += lazy[id];
		}

		//restart the lazy value
		lazy[id] = 0;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		push(id, tl , tm);
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return 0;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			push(id, tl , tm);
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}

	void update(Long l, Long r, Long val, Long id , Long tl , Long tr) { //O(logn)
		if(tr < l || tl > r){
			return;
		}
		if (l <= tl && tr <= r) {
			if(t[id] != 0){
				t[id] += val;
				lazy[id] += val;
			}
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			push(id, tl , tm);
			update(l, r, val , left, tl, tm);
			update(l, r, val , right, tm + 1, tr);
			t[id] = combine(t[left], t[right]);
		}
	}

	void update(Long l , Long r, Long val) {
		assert(maxN > 0);
		update(l, r , val , 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(Interval &A, Interval &B){
	if(A.r == B.r) return A.l > B.l;
	return A.r < B.r;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);

	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
	}

	REP(i , 2)  st[i].build(2 * n);

	sort(v.begin() , v.end(), cmp);

	for(Long i = n - 1; i >= 0; i--){
		Long cur = 1 + max(st[v[i].c].query(v[i].l + 1 , 2 * n - 1) , st[1 - v[i].c].query(v[i].r + 1, 2 * n - 1));
		st[v[i].c].update(0, v[i].l  , 1);
		st[v[i].c].update(v[i].l , cur);
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1)) << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long INF = 1e18;
const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long maxN;

	void clear(Long n) {
		for(Long i = 0; i < 2 * n; i++) {
			t[i] = -INF;
		}
		maxN = n;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return -INF;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(const Interval &A, const Interval &B){
	return A.r < B.r;
}

vector<Long> acum[2];
vector<Long> l[2];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);
	sort(v.begin() , v.end(), cmp);

	acum[0].pb(0);
	acum[1].pb(0);
	st[0].clear(2 * n);
	st[1].clear(2 * n);
	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
		acum[v[i].c].pb(acum[v[i].c].back() + 1);
		acum[1 - v[i].c].pb(acum[1 - v[i].c].back());
		l[v[i].c].pb(v[i].l);
	}

	for(Long i = n - 1; i >= 0; i--){
		Long op = 1 - v[i].c;
		Long cur = st[op].query(v[i].r + 1 , 2 * n - 1);

		if(cur == -INF){
			cur = 0;
		} else{
			Long sz = lower_bound(l[op].begin(), l[op].end(), v[i].r + 1) - l[op].begin();
			cur -= sz;
		}

		Long extra = acum[v[i].c][i + 1];
		st[v[i].c].update(v[i].l , cur + extra );

		/*debug(i);
		debug(v[i].l);
		debug(v[i].r);
		debug(v[i].c);
		debug(cur);
		debug(extra);
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1));

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long INF = 1e18;
const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long maxN;

	void clear(Long n) {
		for(Long i = 0; i < 2 * n; i++) {
			t[i] = -INF;
		}
		maxN = n;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return -INF;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(const Interval &A, const Interval &B){
	return A.r < B.r;
}

Long acum[2][MX];
vector<Long> r[2];

Long greaterEqualThan(vector<Long> &v, Long x){
	return (Long)v.size() - (lower_bound(v.begin(), v.end(), x - 1) - v.begin() );
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);
	sort(v.begin() , v.end(), cmp);

	st[0].clear(2 * n);
	st[1].clear(2 * n);
	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
		r[v[i].c].pb(v[i].r);
	}

	for(Long i = n - 1; i >= 0; i--){
		acum[v[i].c][i] = acum[v[i].c][i + 1] + 1;
		acum[1 - v[i].c][i] = acum[1 - v[i].c][i + 1];
	}

	for(Long i = 0; i < n; i++){
		Long op = 1 - v[i].c;
		Long cur = st[op].query(0 , v[i].l - 1);

		if(cur == -INF){
			cur = 0;
		} else{
			cur -= greaterEqualThan(r[op], v[i].l);
		}

		Long extra = acum[v[i].c][i];
		st[v[i].c].update(v[i].r , cur + extra );

		/*debug(i);
		debug(v[i].l);
		debug(v[i].r);
		debug(v[i].c);
		debug(cur);
		debug(extra);
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1));

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long INF = 1e18;
const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long maxN;

	void clear(Long n) {
		for(Long i = 0; i < 2 * n; i++) {
			t[i] = -INF;
		}
		maxN = n;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return -INF;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(const Interval &A, const Interval &B){
	return A.r < B.r;
}

vector<Long> acum[2];
vector<Long> l[2];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);
	sort(v.begin() , v.end(), cmp);

	acum[0].pb(0);
	acum[1].pb(0);
	st[0].clear(2 * n);
	st[1].clear(2 * n);
	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
		acum[v[i].c].pb(acum[v[i].c].back() + 1);
		acum[1 - v[i].c].pb(acum[1 - v[i].c].back());
		l[v[i].c].pb(v[i].l);
	}
	sort(l[0].begin(), l[0].end());
	sort(l[1].begin(), l[1].end());
	for(Long i = n - 1; i >= 0; i--){
		Long op = 1 - v[i].c;
		Long cur = st[op].query(v[i].r + 1 , 2 * n - 1);

		if(cur == -INF){
			cur = 0;
		} else{
			Long sz = lower_bound(l[op].begin(), l[op].end(), v[i].r + 1) - l[op].begin();
			cur -= sz;
		}

		Long extra = acum[v[i].c][i + 1];
		st[v[i].c].update(v[i].l , cur + extra );

		/*debug(i);
		debug(v[i].l);
		debug(v[i].r);
		debug(v[i].c);
		debug(cur);
		debug(extra);
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1));

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long lazy[2 * MX];
	Long maxN;

	void build(Long n) {
		maxN = n;
	}

	void push(Long id, Long tl , Long tm) { //O(1)
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;

		if(t[left] != 0){
			//Apply the lazy value of the node to the children
			t[left] += lazy[id];
			//aggregate the lazy value of the node to the lazy value of the children
			lazy[left] += lazy[id];
		}
		if(t[right] != 0){
			//Apply the lazy value of the node to the children
			t[right] += lazy[id];
			//aggregate the lazy value of the node to the lazy value of the children
			lazy[right] += lazy[id];
		}

		//restart the lazy value
		lazy[id] = 0;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		push(id, tl , tm);
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return 0;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			push(id, tl , tm);
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}

	void update(Long l, Long r, Long val, Long id , Long tl , Long tr) { //O(logn)
		if(tr < l || tl > r){
			return;
		}
		if (l <= tl && tr <= r) {
			if(t[id] != 0){
				t[id] += val;
				lazy[id] += val;
			}
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			push(id, tl , tm);
			update(l, r, val , left, tl, tm);
			update(l, r, val , right, tm + 1, tr);
			t[id] = combine(t[left], t[right]);
		}
	}

	void update(Long l , Long r, Long val) {
		assert(maxN > 0);
		update(l, r , val , 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(Interval &A, Interval &B){
	if(A.r == B.r) return A.l > B.l;
	return A.r < B.r;
}

/*
3
1 5 1
2 3 1
4 6 2
ans = 2

5
4 5 1
2 7 1
8 10 1
1 3 2
6 9 2
ans = 3

2
1 4 1
2 3 1
ans = 2

3
1 5 1
3 4 1
2 6 1
ans = 3

*/

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);

	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
	}

	REP(i , 2)  st[i].build(2 * n);

	sort(v.begin() , v.end(), cmp);

	for(Long i = n - 1; i >= 0; i--){
		Long cur = 1 + max(st[v[i].c].query(v[i].l + 1 , 2 * n - 1) , st[1 - v[i].c].query(v[i].r + 1, 2 * n - 1));
		st[v[i].c].update(0, v[i].l  , 1);
		st[v[i].c].update(v[i].l , cur);
		/*cout << "[ " << v[i].l << " , " << v[i].r << " ] c = " << v[i].c << endl;
		debug(cur);
		debug(st[v[i].c].query(v[i].l + 1 , 2 * n - 1) );
		debug(st[1 - v[i].c].query(v[i].r + 1, 2 * n - 1));
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1)) << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long INF = 1e18;
const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long maxN;

	void clear(Long n) {
		for(Long i = 0; i < 2 * n; i++) {
			t[i] = -INF;
		}
		maxN = n;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return -INF;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(const Interval &A, const Interval &B){
	if(A.r == B.r){
		return A.l < B.l;
	}
	return A.r < B.r;
}

vector<Long> acum[2];
vector<Long> l[2];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);
	sort(v.begin() , v.end(), cmp);

	acum[0].pb(0);
	acum[1].pb(0);
	st[0].clear(2 * n);
	st[1].clear(2 * n);
	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
		acum[v[i].c].pb(acum[v[i].c].back() + 1);
		acum[1 - v[i].c].pb(acum[1 - v[i].c].back());
		l[v[i].c].pb(v[i].l);
	}
	sort(l[0].begin(), l[0].end());
	sort(l[1].begin(), l[1].end());
	for(Long i = n - 1; i >= 0; i--){
		Long op = 1 - v[i].c;
		Long cur = st[op].query(v[i].r + 1 , 2 * n - 1);

		if(cur == -INF){
			cur = 0;
		} else{
			Long sz = lower_bound(l[op].begin(), l[op].end(), v[i].r + 1) - l[op].begin();
			cur -= sz;
			/*debug(i);
			debug(v[i].l);
			debug(v[i].r);
			debug(v[i].c);
			debug(cur);*/
			assert(cur > 0);
		}

		Long extra = acum[v[i].c][i + 1];
		st[v[i].c].update(v[i].l , cur + extra );

		/*debug(i);
		debug(v[i].l);
		debug(v[i].r);
		debug(v[i].c);
		debug(cur);
		debug(extra);
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1));

	return 0;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i,n) for(Long i = 0; i < (Long)n; i++)
#define pb push_back
using namespace std;

typedef long long Long;

const Long INF = 1e18;
const Long MX = 4e5 + 1;
Long combine(Long x , Long y) {
	return max(x , y);
}

struct SegmentTree{
	Long t[2 * MX];
	Long maxN;

	void clear(Long n) {
		for(Long i = 0; i < 2 * n; i++) {
			t[i] = -INF;
		}
		maxN = n;
	}

	Long query(Long l, Long r, Long id , Long tl , Long tr ) { //O(logn)
		if (l <= tl && tr <= r) {
			return t[id];
		}
		Long tm = (tl + tr) / 2;
		Long left = id + 1;
		Long right = id + 2 * (tm - tl + 1) ;
		if(r < tm + 1){
			//only left child
			return query(l , r , left , tl , tm);
		}else if(tm < l){
			//only right child
			return query(l , r, right , tm + 1 , tr);
		} else{
			//both children
			return combine(query(l, r, left, tl, tm) , query(l, r, right, tm + 1, tr));
		}
	}

	Long query(Long l , Long r) {
		assert(maxN > 0);
		if(l > r) return -INF;
		return query(l , r , 1 , 0 , maxN - 1);
	}

	void update(Long pos, Long val, Long id, Long tl , Long tr) { //O(logn)
		if (tl == tr) {
			t[id] = max(val , t[id]);
		}else{
			Long tm = (tl + tr) / 2;
			Long left = id + 1;
			Long right = id + 2 * (tm - tl + 1) ;
			if (pos <= tm) {
				update(pos, val, left, tl, tm);
			}else {
				update(pos, val, right, tm + 1, tr);
			}
			t[id] = combine(t[left] , t[right] );
		}
	}

	void update(Long pos, Long val) {
		assert(maxN > 0);
		update(pos , val, 1 , 0 , maxN - 1);
	}
} st[2];

struct Interval{
	Long l ,r , c;
};

bool cmp(const Interval &A, const Interval &B){
	return A.r < B.r;
}

Long acum[2][MX];
vector<Long> r[2];

Long greaterEqualThan(vector<Long> &v, Long x){
	return (Long)v.size() - (lower_bound(v.begin(), v.end(), x ) - v.begin() );
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	Long n;
	cin >> n;
	vector<Interval> v;
	vector<Long> compression;
	REP(i , n){
		Interval A;
		cin >> A.l >> A.r >> A.c;
		compression.pb(A.l);
		compression.pb(A.r);
		A.c--;
		v.pb(A);
	}
	sort(compression.begin(), compression.end());
	auto it = unique(compression.begin(), compression.end());
	Long sz = it - compression.begin();
	compression.resize(sz);
	sort(v.begin() , v.end(), cmp);

	st[0].clear(2 * n);
	st[1].clear(2 * n);
	REP(i , n){
		v[i].l = lower_bound(compression.begin(), compression.end(), v[i].l) - compression.begin();
		v[i].r = lower_bound(compression.begin(), compression.end(), v[i].r) - compression.begin();
		r[v[i].c].pb(v[i].r);
	}

	for(Long i = n - 1; i >= 0; i--){
		acum[v[i].c][i] = acum[v[i].c][i + 1] + 1;
		acum[1 - v[i].c][i] = acum[1 - v[i].c][i + 1];
	}

	for(Long i = 0; i < n; i++){
		Long op = 1 - v[i].c;
		Long cur = st[op].query(0 , v[i].l - 1);

		if(cur == -INF){
			cur = 0;
		} else{
			cur -= greaterEqualThan(r[op], v[i].l);
		}

		Long extra = acum[v[i].c][i];
		st[v[i].c].update(v[i].r , cur + extra );

		/*debug(i);
		debug(v[i].l);
		debug(v[i].r);
		debug(v[i].c);
		debug(cur);
		debug(extra);
		cout << endl;*/
	}
	cout << max(st[0].query(0 , 2 * n - 1) , st[1].query(0 , 2 * n - 1));

	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define LD long double
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
int const MAXN = 2e6 + 9;
int l[MAXN], r[MAXN], t[MAXN];
int main(){
    ios_base::sync_with_stdio (0),cin.tie(0);
    int n;
    cin >> n;
    vector<pair<int,int>> inv;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> t[i];
        t[i]--;
       inv.emplace_back(l[i], i), inv.emplace_back(r[i] + 1, -i);
    }
    sort(all(inv));
    multiset<int> black, red;
    int matching = 0;
    for(int i = 0; i < 2*n; i++){
        int x = inv[i].first;
        int idx = inv[i].second;
        if(idx < 0){
            idx *= -1;
            if(t[idx]){
                auto it = black.find(r[idx]);
                if(it != black.end()){
                    black.erase(it);
                    if(sz(red)) matching++, red.erase(red.begin());
                }
            }else {
                auto it = red.find(r[idx]);
                if(it != red.end()){
                    red.erase(it);
                    if(sz(black)) matching++, black.erase(black.begin());

                }
            }
        }else {
            if(t[idx]){
                black.insert(r[idx]);
            }else {
                red.insert(r[idx]);
            }
        }
    }
    cout << n - matching << endl;
}


#include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define LD long double
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
int const MAXN = 2e6 + 9;
int l[MAXN], r[MAXN], t[MAXN];
int main(){
    ios_base::sync_with_stdio (0),cin.tie(0);
    int n;
    cin >> n;
    vector<pair<int,int>> inv;
    for(int i = 1; i <= n; i++){
        cin >> l[i] >> r[i] >> t[i];
        t[i]--;
       inv.emplace_back(l[i], i), inv.emplace_back(r[i] + 1, -i);
    }
    sort(all(inv));
    multiset<int> black, red;
    int matching = 0;
    for(int i = 0; i < 2*n; i++){
        int x = inv[i].first;
        int idx = inv[i].second;
        if(idx < 0){
            idx *= -1;
            if(t[idx]){
                auto it = black.find(r[idx]);
                if(it != black.end())
                black.erase(it);
            }else {
                auto it = red.find(r[idx]);
                if(it != red.end())
                red.erase(it);
            }
        }else {
            if(t[idx]){
                black.insert(r[idx]);
                if(sz(red)) {
                    red.erase(red.begin());
                    matching++;
                }
            }else {
                red.insert(r[idx]);
                if(sz(black)){
                    black.erase(black.begin());
                    matching++;
                }
            }
        }
    }
    cout << n - matching << endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6+5;
vector<pair<int,int>> v1;
set<pair<int,int>> s1[4];
int l[MAXN];
int r[MAXN];
int col[MAXN];
int main() {

    int n;
    cin>>n;
    for(int i=1;i<=n;i++){

        cin>>l[i]>>r[i]>>col[i];
        col[i]--;
        v1.push_back(make_pair(l[i],i));
        v1.push_back(make_pair(r[i],i));
    }
    sort(v1.begin(),v1.end());
    int ans = 0;
    for(auto x:v1){

        int i = x.second;

        if(r[i] == x.first){
          //  cout<<s1[col[i]].count(make_pair(l[i],i))<<endl;
            if(s1[col[i]].count(make_pair(l[i],i)) && s1[1-col[i]].size()){

                s1[1-col[i]].erase(s1[1-col[i]].begin());
                ans++;
            }
            if(s1[col[i]].count(make_pair(l[i],i))){
                s1[col[i]].erase(make_pair(l[i],i));
            }
        }else{
            s1[col[i]].insert(make_pair(l[i],i));
        }
    }
    cout<<n-ans<<endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6+5;
vector<pair<int,pair<int,int>>> v1;
set<pair<int,int>> s1[4];
int l[MAXN];
int r[MAXN];
int col[MAXN];
int main() {

    int n;
    cin>>n;
    for(int i=1;i<=n;i++){

        cin>>l[i]>>r[i]>>col[i];
        col[i]--;
        v1.push_back(make_pair(l[i],make_pair(0,i)));
        v1.push_back(make_pair(r[i],make_pair(1,i)));
    }
    sort(v1.begin(),v1.end());
    int ans = 0;
    for(auto x:v1){

        int i = x.second.second;

        if(x.second.first){
          //  cout<<s1[col[i]].count(make_pair(l[i],i))<<endl;
            if(s1[col[i]].count(make_pair(r[i],i)) && s1[1-col[i]].size()){

                s1[1-col[i]].erase(s1[1-col[i]].begin());
                ans++;
            }
            if(s1[col[i]].count(make_pair(r[i],i))){
                s1[col[i]].erase(make_pair(r[i],i));
            }
        }else{
            s1[col[i]].insert(make_pair(r[i],i));
        }
    }
    cout<<n-ans<<endl;
}


///****In the name of ALLAH, most Gracious, most Compassionate****//

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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

typedef long long ll;
typedef pair <int, int> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const int N=4e5+5;
const int oo=1e9+7;

int l[N];
int r[N];
int t[N];

class segtree
{
public :
    int value[4*N];
    int lazy[4*N];
    void UpdateRange(int node, int ss, int se, int us,int ue, int diff)
    {
        int mid = ss+(se-ss)/2;

        if (lazy[node] != 0)
        {
            value[node] += lazy[node];
            if (ss != se)
            {
                lazy[2*node]+=lazy[node];
                lazy[2*node+1]+=lazy[node];
            }
            lazy[node] = 0;
        }

        if (ss>se || ss>ue || se<us)
            return ;

        if (ss>=us && se<=ue)
        {
            value[node] += diff;

            if (ss != se)
            {
                lazy[2*node]+=diff;
                lazy[2*node+1]+=diff;
            }
            return;
        }

        UpdateRange(2*node, ss, mid, us, ue, diff);
        UpdateRange(2*node+1, mid+1, se, us, ue, diff);

        value[node]=max(value[2*node],value[2*node+1]);

    }
    int GetSum(int node,int ss, int se, int qs, int qe)
    {

        int mid = ss+(se-ss)/2;

        if (lazy[node] != 0)
        {
            value[node] += lazy[node];
            if (ss != se)
            {
                lazy[2*node]+=lazy[node];
                lazy[2*node+1]+=lazy[node];
            }
            lazy[node] = 0;
        }

        if (ss>se || ss>qe || se<qs)
            return 0;

        if (ss>=qs && se<=qe)
            return value[node];

        return max(GetSum(2*node,ss,mid,qs,qe),GetSum(2*node+1,mid+1,se,qs,qe));
    }

} sg[2];

int dp[2][N];
vector<int > rht[2][N];

int32_t main()
{
    FastIO;
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>l[i]>>r[i]>>t[i];
    vector<int > x;
    for(int i=1; i<=n; i++)
    {
        x.push_back(l[i]);
        x.push_back(r[i]);
    }
    sort(ALL(x));
    x.erase(unique(ALL(x)),x.end() );

    for(int i=1; i<=n; i++)
    {
        l[i] = lower_bound(ALL(x), l[i]) - x.begin() + 1;
        r[i] = lower_bound(ALL(x), r[i]) - x.begin() + 1;

        t[i]--;

        rht[t[i]][l[i]].push_back(r[i]);

//        DBG(l[i]);
//        DBG(r[i]);
//        NL;
    }

    int nn = x.size();
    for(int i=nn; i>=1; i--)
    {
        for(int j=0; j<2; j++)
        {
            for(int k:rht[j][i])
            {
                sg[j].UpdateRange(1,1,nn, k, nn, 1);
            }
        }

        for(int j=0; j<2; j++)
        {
            dp[j][i] = max(dp[j][i], sg[j].GetSum(1,1,nn,i,nn));
            sg[!j].UpdateRange(1,1,nn,i-1, i-1, dp[j][i]);
        }
    }

    cout<<max(sg[0].GetSum(1,1,nn,1,nn),sg[1].GetSum(1,1,nn,1,nn)  )<<"\n";
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXSEG = 200000;
const int MAXX = 2 * MAXSEG;
const int MAXNODE = 4 * MAXX;
struct Seg { int l, r, col; };
bool operator<(const Seg& a, const Seg& b) { return a.l < b.l; }

int nseg;
Seg seg[MAXSEG];

struct STree {
	int lazy[MAXNODE];
	int val[MAXNODE];
	void apply(int x, int by) { lazy[x] += by; val[x] += by; }
	void push(int x) { if (lazy[x] != 0) apply(2 * x + 1, lazy[x]), apply(2 * x + 2, lazy[x]), lazy[x] = 0; }
	void pull(int x) { val[x] = max(val[2 * x + 1], val[2 * x + 2]); }
	void init(int x, int l, int r) {
		lazy[x] = 0;
		if (l == r) {
			val[x] = 0;
		} else {
			int m = l + (r - l) / 2;
			init(2 * x + 1, l, m);
			init(2 * x + 2, m + 1, r);
			pull(x);
		}
	}
	void inc(int x, int l, int r, int L, int R) {
		if (L <= l && r <= R) {
			apply(x, 1);
		} else {
			int m = l + (r - l) / 2; push(x);
			if (L <= m) inc(2 * x + 1, l, m, L, R);
			if (m + 1 <= R) inc(2 * x + 2, m + 1, r, L, R);
			pull(x);
		}
	}
	void upd(int x, int l, int r, int IDX, int VAL) {
		if (l == r) {
			val[x] = max(val[x], VAL);
		} else {
			int m = l + (r - l) / 2; push(x);
			if (IDX <= m) upd(2 * x + 1, l, m, IDX, VAL); else upd(2 * x + 2, m + 1, r, IDX, VAL);
			pull(x);
		}
	}
	int get(int x, int l, int r, int L, int R) {
		if (L <= l && r <= R) {
			return val[x];
		} else {
			int m = l + (r - l) / 2; push(x); int ret = 0;
			if (L <= m) ret = max(ret, get(2 * x + 1, l, m, L, R));
			if (m + 1 <= R) ret = max(ret, get(2 * x + 2, m + 1, r, L, R));
			return ret;
		}
	}
};

int x[MAXX], nx;
STree tree[2];

int solve() {
	sort(seg, seg + nseg);
	nx = 0; REP(i, nseg) x[nx++] = seg[i].l, x[nx++] = seg[i].r; sort(x, x + nx); nx = unique(x, x + nx) - x; REP(i, nseg) seg[i].l = lower_bound(x, x + nx, seg[i].l) - x, seg[i].r = lower_bound(x, x + nx, seg[i].r) - x;
	REP(col, 2) tree[col].init(0, 0, nx - 1);
	REP(i, nseg) {
		int l = seg[i].l, r = seg[i].r, col = seg[i].col;
		if (r + 1 < nx) tree[col].inc(0, 0, nx - 1, r + 1, nx);
		int v1 = tree[col].get(0, 0, nx - 1, 0, r);
		int v2 = l - 1 >= 0 ? tree[1 - col].get(0, 0, nx - 1, 0, l - 1) : 0;
		//printf("%d %d %d (v1=%d v2=%d)\n", l, r, col, v1, v2);
		tree[col].upd(0, 0, nx - 1, r, max(v1, v2) + 1);
	}
	int ret = 0;
	REP(col, 2) ret = max(ret, tree[col].get(0, 0, nx - 1, 0, nx - 1));
	return ret;
}

void run() {
	scanf("%d", &nseg);
	REP(i, nseg) scanf("%d%d%d", &seg[i].l, &seg[i].r, &seg[i].col), --seg[i].col;
	printf("%d\n", solve());
}

Seg oseg[MAXSEG];

int solvestupid() {
	int ret = 0;
	REP(mask, 1 << nseg) {
		bool ok = true;
		int cur = 0; REP(i, nseg) if (mask & (1 << i)) ++cur;
		if (cur <= ret) continue;
		REP(i, nseg) if (mask & (1 << i)) FOR(j, i + 1, nseg) if (mask & (1 << j)) if (oseg[i].col != oseg[j].col && oseg[i].l <= oseg[j].r && oseg[j].l <= oseg[i].r) ok = false;
		if (ok) ret = cur;
	}
	return ret;
}

void stress() {
	REP(rep, 10000) {
		nseg = rnd() % 10 + 1;
		int rng = rnd() % 100 + 1;
		REP(i, nseg) { seg[i].l = rnd() % rng, seg[i].r = rnd() % rng, seg[i].col = rnd() % 2; if (seg[i].l > seg[i].r) swap(seg[i].l, seg[i].r); oseg[i] = seg[i]; }
		int have = solve();
		int want = solvestupid();
		if (have == want) { printf("."); continue; }
		printf("err have=%d want=%d\n", have, want);
		printf("%d\n", nseg);
		REP(i, nseg) printf("%d %d %d\n", oseg[i].l, oseg[i].r, oseg[i].col + 1);
		break;
	}
}

int main() {
	run();
	//stress();
	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXSEG = 200000;
const int MAXX = 2 * MAXSEG;
const int MAXNODE = 4 * MAXX;
struct Seg { int l, r, col; };
bool operator<(const Seg& a, const Seg& b) { return a.l < b.l; }

int nseg;
Seg seg[MAXSEG];

struct STree {
	int lazy[MAXNODE];
	int val[MAXNODE];
	void apply(int x, int by) { lazy[x] += by; val[x] += by; }
	void push(int x) { if (lazy[x] != 0) apply(2 * x + 1, lazy[x]), apply(2 * x + 2, lazy[x]), lazy[x] = 0; }
	void pull(int x) { val[x] = max(val[2 * x + 1], val[2 * x + 2]); }
	void init(int x, int l, int r) {
		lazy[x] = 0;
		if (l == r) {
			val[x] = 0;
		} else {
			int m = l + (r - l) / 2;
			init(2 * x + 1, l, m);
			init(2 * x + 2, m + 1, r);
			pull(x);
		}
	}
	void inc(int x, int l, int r, int L, int R) {
		if (L <= l && r <= R) {
			apply(x, 1);
		} else {
			int m = l + (r - l) / 2; push(x);
			if (L <= m) inc(2 * x + 1, l, m, L, R);
			if (m + 1 <= R) inc(2 * x + 2, m + 1, r, L, R);
			pull(x);
		}
	}
	void upd(int x, int l, int r, int IDX, int VAL) {
		if (l == r) {
			val[x] = max(val[x], VAL);
		} else {
			int m = l + (r - l) / 2; push(x);
			if (IDX <= m) upd(2 * x + 1, l, m, IDX, VAL); else upd(2 * x + 2, m + 1, r, IDX, VAL);
			pull(x);
		}
	}
	int get(int x, int l, int r, int L, int R) {
		if (L <= l && r <= R) {
			return val[x];
		} else {
			int m = l + (r - l) / 2; push(x); int ret = 0;
			if (L <= m) ret = max(ret, get(2 * x + 1, l, m, L, R));
			if (m + 1 <= R) ret = max(ret, get(2 * x + 2, m + 1, r, L, R));
			return ret;
		}
	}
};

int x[MAXX], nx;
STree tree[2];

int solve() {
	sort(seg, seg + nseg);
	nx = 0; REP(i, nseg) x[nx++] = seg[i].l, x[nx++] = seg[i].r; sort(x, x + nx); nx = unique(x, x + nx) - x; REP(i, nseg) seg[i].l = lower_bound(x, x + nx, seg[i].l) - x, seg[i].r = lower_bound(x, x + nx, seg[i].r) - x;
	REP(col, 2) tree[col].init(0, 0, nx - 1);
	REP(i, nseg) {
		int l = seg[i].l, r = seg[i].r, col = seg[i].col;
		if (r + 1 < nx) tree[col].inc(0, 0, nx - 1, r + 1, nx);
		int v1 = tree[col].get(0, 0, nx - 1, 0, r);
		int v2 = l - 1 >= 0 ? tree[1 - col].get(0, 0, nx - 1, 0, l) : 0;
		tree[col].upd(0, 0, nx - 1, r, max(v1, v2) + 1);
	}
	int ret = 0;
	REP(col, 2) ret = max(ret, tree[col].get(0, 0, nx - 1, 0, nx - 1));
	return ret;
}

void run() {
	scanf("%d", &nseg);
	REP(i, nseg) scanf("%d%d%d", &seg[i].l, &seg[i].r, &seg[i].col), --seg[i].col;
	printf("%d\n", solve());
}

int main() {
	run();
	return 0;
}


/*
    author : pshishod2645
    Created at : 07:34:07 PM, 31 July 2020
*/

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

typedef long long ll;
//#define int long long
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1;  int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}

struct SegmentTree{
    vector<int> t, lazy; int n;
    void setSize(int _n){
        n = _n;
        t.resize(4*n + 5); lazy.resize(4*n + 5);
    }

    #define mid ((start + end)/2)
    inline void push(int node, bool leaf){
        t[node] += lazy[node];
        if(!leaf){
            lazy[2*node] += lazy[node];
            lazy[2*node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }

    void update(int node, int start, int end, int l, int r, int val){
        push(node, start == end);
        if(start > r || end < l)return ;
        if(start >= l && end <= r){
            lazy[node] += val; push(node, start == end); return;
        }
        update(2*node, start, mid, l, r, val); update(2*node + 1, mid + 1, end, l, r, val);
        t[node] = max(t[2*node], t[2*node + 1]);
    }

    int query(int node, int start, int end, int l, int r){
        push(node, start == end);
        if(start > r || end < l)return 0;
        if(start >= l && end <= r)return t[node];
        return max(query(2*node, start, mid, l, r),
                    query(2*node + 1, mid + 1, end, l, r));
    }

    void update(int l, int r, int val = 1){
        update(1, 0, n - 1, l, r, val);
    }
    int query(int l, int r){
        return query(1, 0, n - 1, l, r);
    }
    #undef mid
}seg[2];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n; cin>>n;
    vector<int> l(n), r(n), t(n), store;
    for(int i = 0;i < n; ++i){
        cin>>l[i]>>r[i]>>t[i]; t[i]--;
        store.push_back(l[i]); store.push_back(r[i]);
    }
    sort(all(store)); store.resize(unique(all(store)) - store.begin());
    map<int, int> mp; int sz = store.size();
    for(int i = 0;i < sz; ++i)mp[store[i]] = i + 1;

    for(int i = 0;i < n; ++i)l[i] = mp[l[i]], r[i] = mp[r[i]];
    // compression done

    vector<int> ending[sz + 1];
    for(int i = 0;i < n; ++i)ending[r[i]].push_back(i);

    vector<vector<int> > dp(sz + 1, vector<int> (2));

    // pr(l); pr(r); pr(t);
    seg[0].setSize(sz + 1); seg[1].setSize(sz + 1);
    for(int i = 1;i <= sz; ++i){
        // pr(i, ending[i]);
        for(int j: ending[i])
            seg[!t[j]].update(0, l[j] - 1);

        dp[i][0] = seg[1].query(0, i - 1);
        dp[i][1] = seg[0].query(0, i - 1);

        seg[1].update(i, i, dp[i][1]);
        seg[0].update(i, i, dp[i][0]);
    }

    // pr(dp);
    int ans = max(dp[sz][0], dp[sz][1]);
    cout<<ans<<"\n";
    return 0;
}


/*
    author : pshishod2645
    Created at : 07:34:07 PM, 31 July 2020
*/

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

typedef long long ll;
//#define int long long
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1;  int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}

struct SegmentTree{
    vector<int> t, lazy; int n;
    void setSize(int _n){
        n = _n;
        t.resize(4*n + 5); lazy.resize(4*n + 5);
    }

    #define mid ((start + end)/2)
    inline void push(int node, bool leaf){
        t[node] += lazy[node];
        if(!leaf){
            lazy[2*node] += lazy[node];
            lazy[2*node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }

    void update(int node, int start, int end, int l, int r, int val){
        push(node, start == end);
        if(start > r || end < l)return ;
        if(start >= l && end <= r){
            lazy[node] += val; push(node, start == end); return;
        }
        update(2*node, start, mid, l, r, val); update(2*node + 1, mid + 1, end, l, r, val);
        t[node] = max(t[2*node], t[2*node + 1]);
    }

    void update(int l, int r, int val = 1){
        update(1, 0, n - 1, l, r, val);
    }
    #undef mid
}seg[2];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n; cin>>n;
    vector<int> l(n), r(n), t(n), store;
    for(int i = 0;i < n; ++i){
        cin>>l[i]>>r[i]>>t[i]; t[i]--;
        store.push_back(l[i]); store.push_back(r[i]);
    }
    sort(all(store)); store.resize(unique(all(store)) - store.begin());
    map<int, int> mp; int sz = store.size();
    for(int i = 0;i < sz; ++i)mp[store[i]] = i + 1;

    for(int i = 0;i < n; ++i)l[i] = mp[l[i]], r[i] = mp[r[i]];
    // compression done

    vector<int> ending[sz + 1];
    for(int i = 0;i < n; ++i)ending[r[i]].push_back(i);

    vector<vector<int> > dp(sz + 1, vector<int> (2));

    seg[0].setSize(sz + 1); seg[1].setSize(sz + 1);
    for(int i = 1;i <= sz; ++i){
        for(int j: ending[i])
            seg[!t[j]].update(0, l[j] - 1);

        dp[i][0] = seg[1].t[1];
        dp[i][1] = seg[0].t[1];

        seg[1].update(i, i, dp[i][1]);
        seg[0].update(i, i, dp[i][0]);
    }

    int ans = max(dp[sz][0], dp[sz][1]);
    cout<<ans<<"\n";
    return 0;
}


/*
    author : pshishod2645
    Created at : 07:34:07 PM, 31 July 2020
*/

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

typedef long long ll;
//#define int long long
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1;  int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n; cin>>n;
    vector<int> l(n), r(n), t(n), store;
    for(int i = 0;i < n; ++i){
        cin>>l[i]>>r[i]>>t[i]; t[i]--;
        store.push_back(l[i]); store.push_back(r[i]);
    }
    sort(all(store)); store.resize(unique(all(store)) - store.begin());
    map<int, int> mp;
    for(int i = 0;i < store.size(); ++i)mp[store[i]] = i + 1;

    for(int i = 0;i < n; ++i)l[i] = mp[l[i]], r[i] = mp[r[i]];
    // compression done

    vector<int> ending[2*n + 1];
    for(int i = 0;i < n; ++i)ending[r[i]].push_back(i);

    vector<vector<int> > dp(2*n + 1, vector<int> (2));
    for(int i = 1;i <= 2*n; ++i){
        sort(all(ending[i]), [&](int i, int j){
            return l[i] > l[j];
        });

        dp[i] = dp[i - 1];
        for(int j = 0;j < 2; ++j){
            for(int k: ending[i])if(t[k] == j)dp[i][j]++;

            int cnt = 0;
            for(int k: ending[i])if(t[k] == j){
                cnt++;
                dp[i][j] = max(dp[i][j], cnt + dp[l[k] - 1][!j]);
            }
        }
    }

    int ans = max(dp[2*n][0], dp[2*n][1]);
    cout<<ans<<"\n";
    return 0;
}


/*
    author : pshishod2645
    Created at : 07:34:07 PM, 31 July 2020
*/

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

typedef long long ll;
//#define int long long
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1;  int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}

struct SegmentTree{
    vector<int> t, lazy; int n;
    void setSize(int _n){
        n = _n;
        t.resize(4*n + 5); lazy.resize(4*n + 5);
    }

    #define mid ((start + end)/2)
    inline void push(int node, bool leaf){
        t[node] += lazy[node];
        if(!leaf){
            lazy[2*node] += lazy[node];
            lazy[2*node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }

    void update(int node, int start, int end, int l, int r, int val = 1){
        push(node, start == end);
        if(start > r || end < l)return ;
        if(start >= l && end <= r){
            lazy[node] += val; push(node, start == end); return;
        }
        update(2*node, start, mid, l, r); update(2*node + 1, mid + 1, end, l, r);
        t[node] = max(t[2*node], t[2*node + 1]);
    }

    int query(int node, int start, int end, int l, int r){
        push(node, start == end);
        if(start > r || end < l)return 0;
        if(start >= l && end <= r)return t[node];
        return max(query(2*node, start, mid, l, r),
                    query(2*node + 1, mid + 1, end, l, r));
    }

    void update(int l, int r, int val = 1){
        update(1, 0, n - 1, l, r, val);
    }
    int query(int l, int r){
        return query(1, 0, n - 1, l, r);
    }
    #undef mid
}seg[2];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n; cin>>n;
    vector<int> l(n), r(n), t(n), store;
    for(int i = 0;i < n; ++i){
        cin>>l[i]>>r[i]>>t[i]; t[i]--;
        store.push_back(l[i]); store.push_back(r[i]);
    }
    sort(all(store)); store.resize(unique(all(store)) - store.begin());
    map<int, int> mp; int sz = store.size();
    for(int i = 0;i < sz; ++i)mp[store[i]] = i + 1;

    for(int i = 0;i < n; ++i)l[i] = mp[l[i]], r[i] = mp[r[i]];
    // compression done

    vector<int> ending[sz + 1];
    for(int i = 0;i < n; ++i)ending[r[i]].push_back(i);

    vector<vector<int> > dp(sz + 1, vector<int> (2));

    // pr(l); pr(r); pr(t);
    seg[0].setSize(sz + 1); seg[1].setSize(sz + 1);
    for(int i = 1;i <= sz; ++i){
        for(int j: ending[i])
            seg[t[j]].update(0, l[i] - 1);

        dp[i][0] = seg[1].query(0, i - 1);
        dp[i][1] = seg[0].query(0, i - 1);

        seg[1].update(i, i, dp[i][0]);
        seg[0].update(i, i, dp[i][1]);
    }

    // pr(dp);
    int ans = max(dp[sz][0], dp[sz][1]);
    cout<<ans<<"\n";
    return 0;
}


// Problem: CF1389F Bicolored Segments
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1389F
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//
//#include <immintrin.h>
//#include <emmintrin.h>

#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define LL long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
inline int getplc(int x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
template <typename T>
// erro
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); errl(_it, args); }

void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}
void Solve();
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

//////////////////////////////////////////////////////////////////

struct node{
  int l,r,op;
 friend bool operator<(node a,node b){
    if(a.r!=b.r)return a.r<b.r;
    else return a.l<b.l;
  }
}q[N];

multiset<node> s[2];

void Solve(){
  int n,ans;
  cin>>n;
  ans=n;
  rep(i,1,n){
    cin>>q[i].l>>q[i].r>>q[i].op;
    q[i].op--;
  }
  sort(q+1,q+1+n);
  rep(i,1,n){
    int c=q[i].op;
    if(s[c^1].lower_bound((node){0,q[i].l,0})==s[c^1].end()){
      s[c].insert(q[i]);
    }
    else{
      auto t=s[c^1].lower_bound((node){0,q[i].l,0});
      s[c^1].erase(t);
      ans--;
    }
  }
  cout<<ans<<endl;
}


// Problem: CF1389F Bicolored Segments
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1389F
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//
//#include <immintrin.h>
//#include <emmintrin.h>

#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define LL long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
inline int getplc(int x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
template <typename T>
// erro
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); errl(_it, args); }

void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}
void Solve();
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

//////////////////////////////////////////////////////////////////

struct node{
  int l,r,op;
 friend bool operator<(node a,node b){
    if(a.r!=b.r)return a.r<b.r;
    else return a.l<a.r;
  }
}q[N];

multiset<node> s[2];

void Solve(){
  int n,ans;
  cin>>n;
  ans=n;
  rep(i,1,n){
    cin>>q[i].l>>q[i].r>>q[i].op;
    q[i].op--;
  }
  sort(q+1,q+1+n);
  rep(i,1,n){
    int c=q[i].op;
    if(s[c^1].lower_bound((node){0,q[i].l,0})==s[c^1].end()){
      s[c].insert(q[i]);
    }
    else{
      auto t=s[c^1].lower_bound((node){0,q[i].l,0});
      s[c^1].erase(t);
      ans--;
    }
  }
  cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ignore ignore
#define pb emplace_back
#define mt make_tuple
#define gcd __gcd
// Input
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define in4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define inp(p) in2(p.F,p.S)
#define llin(a) cin >> a
#define llin2(a,b) cin >> a >> b
#define llin3(a,b,c) cin >> a >> b >> c
#define inl(a) scanf("%lld",&a)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twodread(mat,i,j,n,m) rep(i,n){rep(j,m)in(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
// Output
#define pr(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d\n",a,b)
#define pr3(a,b,c) printf("%d %d %d\n",a,b,c)
#define prp(p) pr2(p.F,p.S)
#define out(a) printf("%d\n",a)
#define outl(a) printf("%lld\n",a)
#define llpr(a) cout << a << " "
#define llpr2(a,b) cout << a << " " << b << " "
#define llout(a) cout << a << "\n"
#define pinttwod(mat,i,j,n,m) rep(i,n){rep(j,m)pr(mat[i][j]);   lin;}
#define plltwod(mat,i,j,n,m)  rep(i,n){rep(j,m)llpr(mat[i][j]); lin;}
#define byes printf("YES\n")
#define bno printf("NO\n")
#define yes printf("Yes\n")
#define no printf("No\n")
#define lin printf("\n")
#define test(q) cout << "Case #" << q << ": "
// Iterator
#define rep(i,n) for(i=0;i<n;++i)
#define fone(i,n) for(i=1;i<=n;++i)
#define rrep(i,n) for(i=n-1;i>=0;--i)
#define lp(i,a,b) for(i=a;i<b;++i)
#define rlp(i,a,b) for(i=a;i>=b;--i)
#define all(vec) vec.begin(),vec.end()
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
// function
#define sz(x) (int)x.size()
#define inrange(i,a,b) (i>=a && i<=b)
#define FLUSH fflush(stdout)
#define precision(x) cout << setprecision(x) << fixed
#define remax(a,b) a=max(a,b)
#define remin(a,b) a=min(a,b)
#define lchild(p) 2*p
#define rchild(p) 2*p+1
#define lseg l,m,2*p
#define rseg m+1,h,2*p+1
#define bhardo(mat,i,j,n,m,t) rep(i,n){rep(j,m)mat[i][j]=t;}
#define bitcount(x)  __builtin_popcount(x)
#define bitcountll(x)  __builtin_popcountll(x)
#define biton(mask,i)  ((mask>>i)&1)
#define bitoff(mask,i) (!((mask>>i)&1))
#define toggle(mask,i) (mask^=(1<<(i)))
#define mul(p,q) ((ll)(p)*(ll)(q))
// Debug
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cerr << *it << " = " << a << "\n";
	err(++it, args...);
}
// Data Type
#define ll long long int
#define ii pair<int,int>
#define pli pair<ll,int>
#define lii pair<ll,ll>
#define triple tuple<int,int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
// Constant
const double PI = acos(-1);
const double eps = (1e-9);
const ll INF = 2e18;
const int M = (1e9)+7;
//const int M= 998244353;
const int N = (4e5)+3;  // check the limit, man
/**
 Have you worked on Simplification of Idea?
 Ok! let's code, check whether it is correct or not?
*/
vvii ad(N);
int ar[2][4*N];
int lazy[2][4*N];
vector<int> compress(vector<int> &temp)
{
    int n=temp.size();
    vector<pair<int,int> > tp(n);
    int i,j,k;
    for(i=0;i<n;i++)
        tp[i].F=temp[i],tp[i].S=i;
    sort(tp.begin(),tp.end());
    vector<int> v(n);
    i=0,k=1,j=0;
    while(i<n)
    {
        while(j<n && tp[i].F==tp[j].F)
            v[tp[j].S]=k,j++;
        i=j,k++;
    }
    return v;
}
void push(int c,int p)
{
    int x=lazy[c][p];
    lazy[c][p]=0;
    ar[c][lchild(p)]+=x;
    ar[c][rchild(p)]+=x;
    lazy[c][lchild(p)]+=x;
    lazy[c][rchild(p)]+=x;
}
void update(int c,int a,int b,int l,int h,int p,int val)
{
    if(a>h || b<l)
        return ;
    if(a<=l && b>=h)
    {
        ar[c][p]+=val,lazy[c][p]+=val;
        return ;
    }
    if(lazy[c][p]>0)
        push(c,p);
    int m=(l+h)/2;
    update(c,a,b,lseg,val);
    update(c,a,b,rseg,val);
    ar[c][p]=max(ar[c][lchild(p)],ar[c][rchild(p)]);
}
void solve()
{
    int i,a,b,c,x,n;
    in(n);
    vi v;
    vi color;
    rep(i,n)
    {
        in3(a,b,c);
        v.pb(a),v.pb(b),color.pb(c-1);
    }
    v=compress(v);
    for(i=0;i<2*n;i+=2)
    {
        a=v[i],b=v[i+1],c=color[i/2];
        ad[b].pb(a,c);
    }
    int ans=0;
    int k=*max_element(all(v));
    fone(i,k)
    {
        for(auto temp : ad[i])
            tie(a,c)=temp,update(c^1,0,a-1,0,k,1,1);
        rep(c,2)
            x=ar[c^1][1],remax(ans,x),update(c,i,i,0,k,1,x);
    }
    out(ans);
}
int main()
{
    int t=1;
//    in(t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
using namespace std;
#define gc() getchar()
#define pc(a) putchar(a)
#define int long long
#define inf (1ll<<60)
inline int read()
{
    int x=0,f=1;
    char c=gc();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-f;
        c=gc();
    }
    while(c>='0'&&c<='9') x=x*10+c-'0',c=gc();
//    cout<<x<<endl;
    return x*f;
}
int nnu[25];
inline void write(int x)
{
    if(x<0) pc('-'),x=-x;
    if(!x)
    {
        pc('0');
        return;
    }
    int tp=0;
    while(x) nnu[++tp]=x%10,x/=10;
    while(tp) pc(nnu[tp--]+'0');
    return;
}
#define ma 500005
//离散化
unordered_map<int,int> mp;
int book[ma],cnt;
inline void ins(int x){if(!mp[x]) mp[x]=1,book[++cnt]=x;}
inline bool lisan(int x,int y){return x<y;}
inline int find(int x)
{
    int l=1,r=cnt;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(book[mid]>=x) r=mid;
        else l=mid+1;
    }
    return l;
}
struct seg
{
    int x,y,c;
    seg(){}
    seg(int xx,int yy,int zz){x=xx,y=yy,c=zz;}
}a[ma];
inline bool cmp(seg x,seg y){return x.y==y.y?x.x>y.x:x.y>y.y;}
int head[ma],sg[ma],nxt[ma],tt;
inline void ins(int x,int y)
{
    sg[++tt]=y;
    nxt[tt]=head[x];
    head[x]=tt;
    return;
}
//线段树
int root[2];
int s[ma<<3],ls[ma<<3],rs[ma<<3],lazy[ma<<3],tot;
inline void New(int &i){i=++tot;}
inline void push_up(int i){s[i]=max(s[ls[i]],s[rs[i]]);}
inline void push_down(int i)
{
    if(!ls[i]) New(ls[i]);
    if(!rs[i]) New(rs[i]);
    lazy[ls[i]]+=lazy[i];
    lazy[rs[i]]+=lazy[i];
    s[ls[i]]+=lazy[i];
    s[rs[i]]+=lazy[i];
    lazy[i]=0;
    return;
}
void add(int &i,int l,int r,int L,int R,int v)
{
    if(!i) New(i);
    if(L<=l&&r<=R)
    {
        s[i]+=v;
        lazy[i]+=v;
        return;
    }
    if(lazy[i]) push_down(i);
    int mid=(l+r)>>1;
    if(L<=mid) add(ls[i],l,mid,L,R,v);
    if(mid+1<=R) add(rs[i],mid+1,r,L,R,v);
    push_up(i);
    return;
}
int sum(int i,int l,int r,int pos)
{
    if(!i) return 0;
    if(l==r)
        return s[i];
    if(lazy[i]) push_down(i);
    int mid=(l+r)>>1;
    if(pos<=mid) return sum(ls[i],l,mid,pos);
    return sum(rs[i],mid+1,r,pos);

}
signed main()
{
//	freopen("rd.in","r",stdin);
//	freopen("my.out","w",stdout);
    int n=read();
    for(int i=1;i<=n;i++)
    {
        int x,y,z;
        ins(x=read()),ins(y=read()),z=read()-1;
        a[i]=seg(x,y,z);
    }
    ins(0);
    sort(book+1,book+cnt,lisan);
    for(int i=1;i<=n;i++)
    {
        a[i].x=find(a[i].x);
        a[i].y=find(a[i].y);
        ins(a[i].y,i);
    }
    for(int i=1;i<cnt;i++)
    {
        for(int j=head[i];j;j=nxt[j])
            add(root[a[sg[j]].c],0,cnt,0,a[sg[j]].x-1,1);
        int m1=s[root[0]],m2=s[root[1]];
        add(root[0],0,cnt,i,i,m2);
        add(root[1],0,cnt,i,i,m1);
    }
    write(max(s[root[0]],s[root[1]]));
}


#include<bits/stdc++.h>
using namespace std;
#define gc() getchar()
#define pc(a) putchar(a)
#define int long long
#define inf (1ll<<60)
inline int read()
{
    int x=0,f=1;
    char c=gc();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-f;
        c=gc();
    }
    while(c>='0'&&c<='9') x=x*10+c-'0',c=gc();
//    cout<<x<<endl;
    return x*f;
}
int nnu[25];
inline void write(int x)
{
    if(x<0) pc('-'),x=-x;
    if(!x)
    {
        pc('0');
        return;
    }
    int tp=0;
    while(x) nnu[++tp]=x%10,x/=10;
    while(tp) pc(nnu[tp--]+'0');
    return;
}
#define ma 500005
//离散化
unordered_map<int,int> mp;
int book[ma],cnt;
inline void ins(int x){if(!mp[x]) mp[x]=1,book[++cnt]=x;}
inline bool lisan(int x,int y){return x<y;}
inline int find(int x)
{
    int l=1,r=cnt;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(book[mid]>=x) r=mid;
        else l=mid+1;
    }
    return l;
}
struct seg
{
    int x,y,c;
    seg(){}
    seg(int xx,int yy,int zz){x=xx,y=yy,c=zz;}
}a[ma];
inline bool cmp(seg x,seg y){return x.y==y.y?x.x>y.x:x.y>y.y;}
int head[ma],sg[ma],nxt[ma],tt;
inline void ins(int x,int y)
{
    sg[++tt]=y;
    nxt[tt]=head[x];
    head[x]=tt;
    return;
}
//线段树
int root[2];
int s[ma<<3],ls[ma<<3],rs[ma<<3],lazy[ma<<3],tot;
inline void New(int &i){i=++tot;}
inline void push_up(int i){s[i]=max(s[ls[i]],s[rs[i]]);}
inline void push_down(int i)
{
    if(!ls[i]) New(ls[i]);
    if(!rs[i]) New(rs[i]);
    lazy[ls[i]]+=lazy[i];
    lazy[rs[i]]+=lazy[i];
    s[ls[i]]+=lazy[i];
    s[rs[i]]+=lazy[i];
    lazy[i]=0;
    return;
}
void add(int &i,int l,int r,int L,int R,int v)
{
    if(!i) New(i);
    if(L<=l&&r<=R)
    {
        s[i]+=v;
        lazy[i]+=v;
        return;
    }
    if(lazy[i]) push_down(i);
    int mid=(l+r)>>1;
    if(L<=mid) add(ls[i],l,mid,L,R,v);
    if(mid+1<=R) add(rs[i],mid+1,r,L,R,v);
    push_up(i);
    return;
}
int sum(int i,int l,int r,int pos)
{
    if(!i) return 0;
    if(l==r)
        return s[i];
    if(lazy[i]) push_down(i);
    int mid=(l+r)>>1;
    if(pos<=mid) return sum(ls[i],l,mid,pos);
    return sum(rs[i],mid+1,r,pos);

}
signed main()
{
// 	freopen("rd.in","r",stdin);
// 	freopen("my.out","w",stdout);
    int n=read();
    for(int i=1;i<=n;i++)
    {
        int x,y,z;
        ins(x=read()),ins(y=read()),z=read()-1;
        a[i]=seg(x,y,z);
    }
    sort(book+1,book+cnt,lisan);
    for(int i=1;i<=n;i++)
    {
        a[i].x=find(a[i].x);
        a[i].y=find(a[i].y);
        ins(a[i].y,i);
    }
    for(int i=1;i<cnt;i++)
    {
        for(int j=head[i];j;j=nxt[j])
            add(root[a[sg[j]].c],0,cnt,0,a[sg[j]].x-1,1);
        int m1=s[root[0]],m2=s[root[1]];
        add(root[0],0,cnt,i,i,m2);
        add(root[1],0,cnt,i,i,m1);
    }
    write(max(s[root[0]],s[root[1]]));
}


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

//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair(a,b)
#define x first
#define y second
#define be(a) a.begin()
#define en(a) a.end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

//Data
const int N=2e5,M=(N<<1)+1;
int n,l[N],r[N],t[N],cnt,b[M],ans;
vector<int> ca[M];

//Segmenttree
const int T=M<<2;
#define lk k<<1
#define rk k<<1|1
struct Segmenttree{ //线段树，下标为坐标，维护区间加、全局最大值
    int mx[T],mk[T];
    void pushup(int k){mx[k]=max(mx[lk],mx[rk]);}
    void pm(int k,int v){mk[k]+=v,mx[k]+=v;}
    void pushdown(int k){if(mk[k]) pm(lk,mk[k]),pm(rk,mk[k]),mk[k]=0;}
    void fix(int x,int y,int v,int k,int l,int r){
        if(x<=l&&r<=y) return pm(k,v);
        pushdown(k);
        int mid=(l+r)>>1;
        if(mid>=x) fix(x,y,v,lk,l,mid);
        if(mid<y) fix(x,y,v,rk,mid+1,r);
        pushup(k);
    }
    int Mx(){return mx[1];}
    void Print(int k,int l,int r){
        if(l==r){cout<<mx[k]<<' ';return;}
        pushdown(k);
        int mid=(l+r)>>1;
        Print(lk,l,mid),Print(rk,mid+1,r);
    }
}g[2];

//Main
int main(){
// 	freopen("rd.in","r",stdin);
// 	freopen("std.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>l[i]>>r[i]>>t[i],--t[i];
        b[cnt++]=l[i],b[cnt++]=r[i];
    }
    b[cnt++]=0,sort(b,b+cnt),cnt=unique(b,b+cnt)-b;
    for(int i=0;i<n;i++){
        l[i]=lower_bound(b,b+cnt,l[i])-b;
        r[i]=lower_bound(b,b+cnt,r[i])-b;
        ca[r[i]].pb(i);
    }
    for(int i=1;i<cnt;i++){
        for(int x:ca[i]) g[t[x]].fix(0,l[x]-1,1,1,0,cnt);
        g[0].fix(i,i,g[1].Mx(),1,0,cnt),g[1].fix(i,i,g[0].Mx(),1,0,cnt);//这么写也是可以的
    }
    cout<<max(g[0].Mx(),g[1].Mx())<<'\n';
    return 0;
}


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

const int MAXN = 200005;

int n, li, ri, ti;

int main(){
	scanf("%d", &n);

	vector < pair <int, int> > v1, v2;
 	for(int i = 0; i < n; i++){
		scanf("%d %d %d", &li, &ri, &ti);

		if(ti == 1){
			v1.push_back(make_pair(li, ri));
		}else{
			v2.push_back(make_pair(li, ri));
		}
	}
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());

	int i = 0, j = 0, res = 0;
	multiset <int> s1, s2;

	while(true){
		int mn = -1;
		if(i < (int)v1.size() && (mn == -1 || v1[i].first < mn))mn = v1[i].first;
		if(j < (int)v2.size() && (mn == -1 || v2[j].first < mn))mn = v2[j].first;

		if(mn == -1)break;

		while(i < (int)v1.size() && mn == v1[i].first){
			s1.insert(v1[i].second);
			i++;
		}
		while(j < (int)v2.size() && mn == v2[j].first){
			s2.insert(v2[j].second);
			j++;
		}

		while(!s1.empty() && (*s1.begin() < mn))s1.erase(s1.begin());
		while(!s2.empty() && (*s2.begin() < mn))s2.erase(s2.begin());

		while(!s1.empty() && !s2.empty()){
			res++;
			s1.erase(s1.begin());
			s2.erase(s2.begin());
		}
	}

	printf("%d\n", n - res);
	return 0;
}


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

int n, li, ri, ti;

int main(){
	scanf("%d", &n);

	vector < pair <int, int> > v1, v2;
	vector <int> vx;

 	for(int i = 0; i < n; i++){
		scanf("%d %d %d", &li, &ri, &ti);

		if(ti == 1){
			v1.push_back(make_pair(li, ri));
		}else{
			v2.push_back(make_pair(li, ri));
		}

		vx.push_back(li);
		vx.push_back(ri);
	}
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	sort(vx.begin(), vx.end());
	vx.resize(unique(vx.begin(), vx.end()) - vx.begin());

	int i = 0, j = 0, res = 0;
	multiset <int> s1, s2;

	for(int x : vx){
		while(i < (int)v1.size() && v1[i].first == x){
			s1.insert(v1[i].second);
			i++;
		}
		while(j < (int)v2.size() && v2[j].first == x){
			s2.insert(v2[j].second);
			j++;
		}

		while(!s1.empty() && (*s1.begin()) == x){
			if(s2.empty()){
				s1.erase(s1.begin());
				continue;
			}

			s1.erase(s1.begin());
			s2.erase(s2.begin());
			res++;
		}

		while(!s2.empty() && (*s2.begin()) == x){
			if(s1.empty()){
				s2.erase(s2.begin());
				continue;
			}

			s1.erase(s1.begin());
			s2.erase(s2.begin());
			res++;
		}
	}

	printf("%d\n", n - res);
	return 0;
}


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

//#define int long long

struct T {
	static const int N = 4e5 + 7;
	int t[N << 2], lzy[N << 2];
	void push(int v, int l, int r) {
		if(lzy[v]) {
			t[v] += lzy[v];
			if(l != r) {
				lzy[v * 2] += lzy[v];
				lzy[v * 2 + 1] += lzy[v];
			}
			lzy[v] = 0;
		}
	}

	void modify(int v, int l, int r, int b, int e, int x) {
		push(v, l, r);
		if(l > e || r < b) {
			return;
		}
		if(l >= b && r <= e) {
			lzy[v] += x;
			push(v, l, r);
			return;
		}
		int mid = (l + r) >> 1;
		modify(v * 2, l, mid, b, e, x);
		modify(v * 2 + 1, mid + 1, r, b, e, x);
		t[v] = max(t[v * 2], t[v * 2 + 1]);

	}

	void update(int v, int l, int r, int p, int x) {
		push(v, l, r);
		if(l == r) {
			t[v] = x;
			return;
		}
		int mid = (l + r) >> 1;
		push(v * 2, l, mid);
		push(v * 2 + 1, mid + 1, r);
		if(p <= mid) update(v * 2, l, mid, p, x);
		else 		 update(v * 2 + 1, mid + 1, r, p, x);
		t[v] = max(t[v * 2], t[v * 2 + 1]);
	}

}W, B;

const int N = 4e5 + 7;

vector <pair <int, int> > Q[N];

int l[N], r[N], t[N], dp[N];

int32_t main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n; cin >> n;
	vector <int> v;
	for(int i = 1; i <= n; i++) {
		cin >> l[i] >> r[i] >> t[i];
		v.push_back(l[i]);
		v.push_back(r[i]);
	}

	sort(v.begin(), v.end()); map <int, int> mp; int ptr = 0;
	for(int x: v) if(!mp[x]) mp[x] = ++ptr;

	for(int i = 1; i <= n; i++) {
		Q[mp[l[i]]].push_back(make_pair(mp[r[i]], t[i]));
	}

	for(int i = ptr; i >= 1; i--) {
		for(auto z: Q[i]) {
			int rx, idx; tie(rx, idx) = z;
			if(idx == 1) B.modify(1, 1, ptr + 1, rx + 1, ptr + 1, 1);
			else 		 W.modify(1, 1, ptr + 1, rx + 1, ptr + 1, 1);
		}
		dp[i] = max({dp[i + 1], B.t[1], W.t[1]});
		B.update(1, 1, ptr + 1, i, dp[i]);
		W.update(1, 1, ptr + 1, i, dp[i]);
	}

	cout << *max_element(dp, dp + N) << endl;
}