#include<bits/stdc++.h>
const int N = 5005;
const long long inf = 1e18;
using namespace std;

typedef pair <int, int> ii;
vector <ii> mv;

int n, ans;
long long dp[N][N], Min[N];

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++){
        int x, y; cin >> x >> y;
        x += y;
        mv.push_back(ii(x, y));
    }
    sort(mv.begin(), mv.end());
    for (int i = 0; i < N; i++) Min[i] = inf;
    Min[0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j >= 1; j--){
            if (mv[i-1].first - mv[i-1].second >= Min[j-1]) dp[i][j] = Min[j-1] + mv[i-1].second;
            else dp[i][j] = inf;
            Min[j] = min(Min[j], dp[i][j]);
            if (dp[i][j] != inf) ans = max(ans, j);
        }
    }
    cout << ans;
}

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

int n, a[15], x[15][15], y[15][15];

int cnt(int i)
{
    if (!i) return 0;
    return (i & 1) + cnt(i >> 1);
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        for (int j = 0; j < a[i]; j++)
        {
            cin >> x[i][j] >> y[i][j];
        }
    }

    int ans = 0;
    for (int bit = 0; bit < (1 << n); bit++)
    {
        int ok = 1;
        for (int i = 0; i < n; i++)
        {
            if ((bit >> i) & 1)
            {
                for (int j = 0; j < a[i]; j++)
                {
                    if (((bit >> (x[i][j] - 1)) & 1) ^ y[i][j]) ok = 0; 
                }
            }
        }
        if (ok) ans = max(ans, cnt(bit));
    }
    cout << ans << "\n";
}