#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL<<32);
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;
typedef pair<ll, ll> P;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

static const int MAX = 100;
ll d[MAX][MAX];

void floyd()
{
    REP(k, n)
    {
        REP(i, n)
        {
            if (d[i][k] == INF)
            {
                continue;
            }
            REP(j, n)
            {
                if (d[k][j] == INF)
                {
                    continue;
                }
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

int main()
{
    ll e, u, v, c;
    cin >> n >> e;
    REP(i, n)
    {
        REP(j, n)
        {
            d[i][j] = ((i == j) ? 0 : INF);
        }
    }
    REP(i, e)
    {
        cin >> u >> v >> c;
        d[u][v] = c;
    }

    floyd();

    bool negative = false;
    REP(i, n)
    {
        if (d[i][i] < 0)
        {
            negative = true;
        }
    }
    if (negative)
    {
        cout << "NEGATIVE CYCLE" << endl;
    }
    else
    {
        REP(i, n)
        {
            REP(j, n)
            {
                if (j)
                {
                    cout << " ";
                }
                if (d[i][j] == INF)
                {
                    cout << "INF";
                }
                else
                {
                    cout << d[i][j];
                }
            }
            cout << endl;
        }
    }
}
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
const int nax = 5010;
const ll INF = 1e18L;
ll dp[nax][nax];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  for (int i = 0; i < nax; ++i) {
    for (int j = 0; j <= i; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;

  int n;
  cin >> n;
  vector<pair<int, int>> q(n);
  for (int i = 0; i < n; ++i) {
    cin >> q[i].first >> q[i].second;
    q[i].first += q[i].second;
  }
  sort(q.begin(), q.end());
  for (int i = 0; i < n; ++i) {
    q[i].first -= q[i].second;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (dp[i][j] == INF) continue;
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (dp[i][j] <= q[i].first) {
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + q[i].second);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (dp[n][i] != INF) {
      ans = i;
    }
  }
  cout << ans << '\n';
}