//Bismillahir Rahmanir Raheem
#ifdef ONLINE_JUDGE
    #include<bits/stdc++.h>
#endif
#include<iostream>
#include<numeric>
#include<tuple>
#include<iomanip>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<math.h>
#include<vector>
#include<climits>
#include<algorithm>
#include<string>
#include<sstream>
#include<string.h>
#include<deque>
#include<set>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(cnt, x) memset((cnt), (x), sizeof(cnt))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int,int> > vii;
typedef pair<int, int> pii;
typedef vector<bool> vbl;
typedef vector<pair<ll,ll> > vll;
typedef pair<ll, ll> pll;
#define PQ priority_queue
#define ff first
#define ss second
#define MP make_pair
#define MT make_tuple
#define mxN (int)2e5 + 2
#define sz(n) (int)(n).size()
#define fasterIO ios::sync_with_stdio(0); cin.tie(0);
#define forc(cn) for(auto &x : (cn))
#define findc(cn, abc) ((cn).find(abc) != (cn).end())

#define PI 2*acos(0)
#define forc(cn) for(auto &x : (cn))
#define MAX 200010
const ll inf = INT_MAX;
const int mod = 1e9 + 7;

bool cmp(const pii x, const pii y) {
    return 2ll * x.ff + x.ss < 2ll * y.ff + y.ss;
}

void solve()
{
    int n;
    cin >> n;
    vii a(n);
    forc(a) 
        cin >> x.ff >> x.ss;
    sort(all(a), cmp);
    ll sa = 0, sb = 0, ans = 0;
    forc(a) sa += x.ff;
    for(int i = n - 1; i >= 0; i--) {
        pii &x = a[i];
        sb += x.ff + x.ss;
        sa -= x.ff;
        ans++;
        if(sb > sa) break;
    }
    cout << ans << endl;
}
int main()
{
    fasterIO
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        freopen("output.o", "w+", stdout);
    #endif
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

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

//#Rohitpratap311
//#Keep Calm And Stay Happy


using ll = long long;
using ld = long double;
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
ll mod=1e9;

ll binpow(long long a, long long b) {
    a %= mod;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

bool ifprime(ll n)
{
    if(n==2||n==3) { return true ; }
    for(ll i=2;i<=sqrt(n);i++)
    {
        if(n%i==0) { return false; }
    }
    return true;
}

vector<ll> prime(1000001,1);
void seive()
{
    prime[0]=0;
    prime[1]=0;
    for(ll i=2;i*i<=1000000;i++)
    {
        if(prime[i])
        {
            for(ll j=i*i;j<=1000000;j+=i)
            {
                prime[j]=0;
            }
        }
    }
}

ll nCr[1001][1001];
void NcR() 
{
    loop(i,0,1001) 
    {
        loop(j,0,1001) 
        {
            nCr[i][j] = 0;
        }
        nCr[i][0] = 1; 
    }
    loop(i,1,1001) 
    {
        loop(j,1,i+1) 
        {
            nCr[i][j]=(nCr[i-1][j]+nCr[i-1][j-1])%mod;
        }
    }
}
ll gcd(ll a,ll b)
{
    if(b==0) { return a; }
    return gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

bool good(ll n)
{
    n=abs(n);
    while(n)
    {
        if(n%10==8) return 1;
        n/=10;
    }
    return false;
}

void solveCP311()
{
    ll n,m;
    cin>>n>>m;
    vector<ll>v(n);
    for(ll i=0;i<n;++i) cin>>v[i];
    set<ll>s;
    vector<ll>cnt(n+1);
    for(ll i=0;i<m;++i) cnt[v[i]]++;
    for(ll i=0;i<=n;++i) if(cnt[i]==0) s.insert(i);
    ll ans=*s.begin();
    for(ll i=1;i<=n-m;++i){
    s.insert(n);
    cnt[v[i-1]]--;
    if(cnt[v[i-1]]==0) s.insert(v[i-1]);
    if(s.find(v[i+m-1])!=s.end()) s.erase(v[i+m-1]);
    cnt[v[i+m-1]]++;
    ans=min(ans,*s.begin());
}
cout<<ans;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    //cin>>t;
    NcR();
    seive();
    while(t--)
    {
        solveCP311();
    }
    return 0;
}