/* Author : Aaryan Srivastava ^__^ */ 
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#define pb push_back
#define mp make_pair
#define ff first
#define ss second   
#define rep(i,n) for(int i = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define setval(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x , a) for(auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll ; 

#define int ll

using namespace std;

const int N = 3e5 +5 ;
const int mod = 1e9 + 7 ;
const ll inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace __gnu_pbds;
using ordered_set =  tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ;

typedef pair<int, int> pii; 
typedef pair<int , pii> ipii ;
typedef pair<pii , int> piii ; 
typedef unsigned long long ull ;
typedef long double ld;

ll po(ll x,ll y,ll p = mod) {ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}

bool cmp(pii& a,  pii& b){
    return 1LL * (a.ff + 2LL * a.ss) > 1LL * (b.ff + 2LL * b.ss);
}

void solve()
{
    int n ;
    cin >> n ;
    vector<pii> v(n);
    rep(i , n) cin >> v[i].ss >> v[i].ff;
    sort(all(v) , cmp);
    int cur = 0;
    rep(i , n) cur -= v[i].ss;
    for(int i = 0 ; i < n ; ++i){
        cur += v[i].ss * 2 + v[i].ff; 
        if(cur > 0){
            cout << i + 1; 
            return; 
        }
    }
}   

     
int32_t main(int32_t argc, char *argv[])
{
    ios::sync_with_stdio(0);    
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case #" << t << ": " ;
        solve();
        cout << "\n" ;
    }
    return 0;
}
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define vt vector
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define sz(x) int(x.size())
#define uset unordered_set
#define umap unordered_map
#define ins insert
const int inf = 0x3f3f3f3f, mod = 1e9+7, mx = 2e5+10;
const ll infll = 0x3f3f3f3f3f3f3f3f;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n; cin >> n;
	vector<array<int, 3>> a;
	for(int i=0; i < n; i++){
		int t, l, r;cin >> t >>l >> r;
		if(t==1){
			a.pb({l, r, t});
		}else if(t==2){
			a.pb({l, r-1, t});
		}else if(t==3){
			a.pb({l+1, r, t});
		}else{
			a.pb({l+1, r-1, t});
		}
	}
	int ans =0 ;
	sort(all(a));
	for(int i=0; i < a.size(); i++){
		for(int j=i+1; j < a.size(); j++){
			auto [l, r, t]= a[i];auto [L, R, T] = a[j];
			if(r >= L){
				ans++; continue;
			}
			int tmp = r, tmp1 = L;
			if(t==2 || t==4){
				tmp++;
			}
			if(T==3||T==4)tmp1--;
			if(tmp > tmp1)ans++;
		}
	}
	cout << ans;
}
