#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pii pair < int, int >
#define pb push_back
#define ll long long
#define int ll

const int inf = (int)1e18 + 7;

pii ar[5234];
int dp[5234];
int x;

bool comp (const pii &a, const pii &b) {
    return a.fr + a.sc < b.fr + b.sc;
}

main () {
    int n; scanf ("%lld", &n);
    for (int i = 1; i <= n; i++) {
        int a, b; scanf ("%lld %lld", &a, &b);
        ar[i] = {a, b};
    }
    for (int i = 0; i <= n; i++) {
        dp[i] = inf;
    }
    sort(ar + 1, ar + n + 1, comp);
    dp[0] = x;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j >= 1; j--) {
            if (ar[i].fr >= dp[j - 1]) {
                dp[j] = min(dp[j], dp[j - 1] + ar[i].sc);
            }
        }
    }
    vector < int > ans;
    while (dp[n] == inf) n--;
    printf ("%lld\n", n);
    return 0;
    while (n > 1) {
        for (int i = n - 1; i >= 1; i--) {
            if (dp[n] == dp[i] + ar[i].sc) {
                cout << i << ' ';
                n = i;
                break;
            }
        }
    }
    for (int to : ans) {
        printf ("%d ", to);
    }
}

#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
#define mod 1000000007
#define mod2 998244353 
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x)  __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(),(x).end()
#define scf(x) scanf("%lld",&x)
#define rep(i,n) for(ll i=0;i<n;i++)
const int INF = (int)1e9;
const ll IINF=1LL<<62;
const int maxn=10000005;

//int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;     
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/
 
/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
/*int find(int a){
     if(a==par[a])return a;
     return par[a]=find(par[a]);
}
void unite(int a,int b){
     a=find(a),b=find(b);
     if(a==b)return ;
     par[b]=a;
}
ll modexpo(ll a,ll b)
{
     ll ans=1LL;
     while(b>0)
     {
          if(b&1)ans=(ans*a)%mod;
          a=(a*a)%mod;
          b>>=1;
     }
     return ans;
}
*/
vector<int> adj[100005];
int indeg[100005];
int par[100005];
int main(){
     int n,m;
     cin>>n>>m;
     for(int i=1;i<=n+m-1;i++){
          int a,b;cin>>a>>b;
          adj[a].pb(b);indeg[b]++;
     }
     queue<int> q;
     for(int i=1;i<=n;i++)if(indeg[i]==0){q.push(i);break;}
     while(!q.empty()){
          int x=q.front();q.pop();
          for(int child:adj[x]){
               indeg[child]--;
               if(indeg[child]==0){
                    q.push(child);par[child]=x;
               }
          }
     }
     for(int i=1;i<=n;i++)cout<<par[i]<<"\n";


}
 
 
 
 























