#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

const int MAXN = 2e5 + 20, MAXM = 1e4 + 20;
int n,m,k;
struct Node{
  int t,a,b;
}node[MAXN];
deque<int> a,b,c,d,e,sa,sb,sc,se,ans;

struct ST{
  int n,cnt[MAXM<<2],v[MAXM<<2];
  void add(int x){
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u], v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x<=m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }
  int qMin(int k){
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    if(l == r) return k * l;
    int m = (l+r)/2, sum = 0;
    sum = qMin(u<<1,l,m,min(k,cnt[u<<1]));
    if(k > cnt[u<<1]) sum += qMin(u<<1|1,m+1,r,k-cnt[u<<1]);
    return sum;
  }
}sg;

void cal(int rec){
  if(rec >= k){
    VR<pii> c, e;
    c.push_back({0,0});
    e.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else e.push_back({t.t, i});
    }
    sort(ALL(c)), sort(ALL(e));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,m-rec) ans.push_back(e[i].SE);
  } else {
    VR<pii> c, a, b, left;
    a.push_back({0,0}); b.push_back({0,0}); c.push_back({0,0}); left.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else if(t.a) a.push_back({t.t, i});
      else if(t.b) b.push_back({t.t, i});
      else left.push_back({t.t, i});
    }
    sort(ALL(a)), sort(ALL(b)), sort(ALL(c));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,k-rec) ans.push_back(a[i].SE);
    FORR(i,1,k-rec) ans.push_back(b[i].SE);

    FOR(i,k-rec+1,a.size()) left.push_back(a[i]);
    FOR(i,k-rec+1,b.size()) left.push_back(b[i]);
    sort(ALL(left)); int r = m - 2*k + rec;
    FORR(i,1,r) ans.push_back(left[i].SE);
  }
}

int main(void){
  scanf("%d%d%d",&n,&m,&k); sg.n = 1e4;
  FORR(i,1,n){
    Node &t = node[i];
    scanf("%d%d%d",&t.t,&t.a,&t.b);
    if(t.a && t.b) c.push_back(t.t);
    else{
      if(t.a) a.push_back(t.t);
      else if(t.b) b.push_back(t.t);
      else d.push_back(t.t);
      e.push_back(t.t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
  int na = a.size(), nb = b.size(), nc = c.size(), nd = d.size(), ne = e.size();
  a.push_front(0); b.push_front(0); c.push_front(0); d.push_front(0); e.push_front(0);
  sa = a, sb = b, sc = c, se = e;
  FORR(i,1,na) sa[i] += sa[i-1];
  FORR(i,1,nb) sb[i] += sb[i-1];
  FORR(i,1,nc) sc[i] += sc[i-1];
  FORR(i,1,ne) se[i] += se[i-1];
#ifdef DEBUG
  printf("a:"); FORR(i,1,na) printf(" %d",a[i]); putchar('\n');
  printf("b:"); FORR(i,1,nb) printf(" %d",b[i]); putchar('\n');
  printf("c:"); FORR(i,1,nc) printf(" %d",c[i]); putchar('\n');
  printf("d:"); FORR(i,1,nd) printf(" %d",d[i]); putchar('\n');
  printf("e:"); FORR(i,1,ne) printf(" %d",e[i]); putchar('\n');
#endif

  int ansMin = INT_MAX, rec = -1;
  if(nc >= k) FORR(i,k,min(nc, m)){
    int sum = sc[i];
    if(ne < m-i) sum = INT_MAX;
    else sum += se[m-i];

    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
  }

  FORR(i,1,nd) sg.add(d[i]);
  bool first = true;
  FORR(i, max(max(0, 2*k-m), max(k-na,k-nb)), min(nc, k-1)){
    if(first){
      FORR(j,k-i+1,na) sg.add(a[j]);
      FORR(j,k-i+1,nb) sg.add(b[j]);
      first = false;
    }

    int sum = sc[i] + sa[k-i] + sb[k-i];
    if(m-2*k+i > sg.cnt[1]) sum = INT_MAX;
    else sum += sg.qMin(m-2*k+i);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
    sg.add(a[k-i]); sg.add(b[k-i]);
  }

  if(ansMin == INT_MAX) { printf("-1\n"); return 0; }
  cal(rec);
  printf("%d\n",ansMin);
  for(int x : ans) printf("%d ",x); putchar('\n');

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
}aa[N],bb[N],cc[N],dd[N]; //dd是都不喜欢
int ta[N],tb[N],td[N];
int sz1,sz2,sz3,sz4;
bool cmp(T a,T b){
	return a.val<b.val;
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1,cmp);
	sort(bb+1,bb+1+sz2,cmp);
	sort(cc+1,cc+1+sz3,cmp);
	sort(dd+1,dd+1+sz4,cmp);
	for(int i=1;i<=sz1;i++) ta[i]=aa[i].val;
	for(int i=1;i<=sz2;i++) tb[i]=bb[i].val;
	for(int i=1;i<=sz4;i++) td[i]=dd[i].val;

	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		if(i>m) break;
		int x=0,y=0,t;
		if(i<k){
			x=y=k-i;
		}
		if(x>sz1||y>sz2) continue;
		t=m-i-x-y;
		if(t<0) continue;
		//要在A B D中取t本
		if(sz1-x+sz2-y+sz4<t) continue;  //不够
		int pA=x+1,pB=y+1,pD=1,cur=0;
		ta[sz1+1]=INF;
		tb[sz2+1]=INF;
		td[sz4+1]=INF;
		while(cur<t){
			if(pA<=sz1&&ta[pA]<=tb[pB]&&ta[pA]<=td[pD]){
				++cur;
				++pA;
				continue;
			}
			if(pB<=sz2&&tb[pB]<=ta[pA]&&tb[pB]<=td[pD]){
				++cur;
				++pB;
				continue;
			}
			if(pD<=sz4&&td[pD]<=ta[pA]&&td[pD]<=tb[pB]){
				++cur;
				++pD;
				continue;
			}
		}
		int sum=aa[pA-1].val+bb[pB-1].val+cc[i].val+dd[pD-1].val;
		if(sum<ans){
			ans=sum;
			aaa=pA-1;
			bbb=pB-1;
			ccc=i;
			ddd=pD-1;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
}aa[N],bb[N],cc[N],dd[N]; //dd是都不喜欢
int sz1,sz2,sz3,sz4;
bool cmp(T a,T b){
	return a.val<b.val;
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1,cmp);
	sort(bb+1,bb+1+sz2,cmp);
	sort(cc+1,cc+1+sz3,cmp);
	sort(dd+1,dd+1+sz4,cmp);
	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		if(i>m) break;
		int x=0,y=0,t;
		if(i<k){
			x=y=k-i;
		}
		if(x>sz1||y>sz2) continue;
		t=m-i-x-y;
		if(t<0) continue;
		//要在A B D中取t本
		if(sz1-x+sz2-y+sz4<t) continue;  //不够
		int pA=x+1,pB=y+1,pD=1,cur=0;
		aa[sz1+1].val=INF;
		bb[sz2+1].val=INF;
		dd[sz4+1].val=INF;
		while(cur<t){
			if(pA<=sz1&&aa[pA].val<=bb[pB].val&&aa[pA].val<=dd[pD].val){
				++cur;
				++pA;
				continue;
			}
			if(pB<=sz2&&bb[pB].val<=aa[pA].val&&bb[pB].val<=dd[pD].val){
				++cur;
				++pB;
				continue;
			}
			if(pD<=sz4&&dd[pD].val<=aa[pA].val&&dd[pD].val<=bb[pB].val){
				++cur;
				++pD;
				continue;
			}
		}
		int sum=aa[pA-1].val+bb[pB-1].val+cc[i].val+dd[pD-1].val;
		if(sum<ans){
			ans=sum;
			aaa=pA-1;
			bbb=pB-1;
			ccc=i;
			ddd=pD-1;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int ta[N],tb[N],td[N];
int n,m,k;

struct T{
	int id,val;
	bool operator < (const T &b) const{
		if(val!=b.val)
			return val<b.val;
		else
			return id<b.id;
	}
}aa[N],bb[N],cc[N],dd[N];
int sz1,sz2,sz3,sz4;

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1);
	sort(bb+1,bb+1+sz2);
	sort(cc+1,cc+1+sz3);
	sort(dd+1,dd+1+sz4);
	for(int i=1;i<=sz1;i++) ta[i]=aa[i].val;
	for(int i=1;i<=sz2;i++) tb[i]=bb[i].val;
	for(int i=1;i<=sz4;i++) td[i]=dd[i].val;

	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		int x=k-i;
		int tt=m-i;
		if(tt-2*x<0) continue;
		if(x>sz1||x>sz2) continue;
		if(sz1+sz2+sz4<tt) continue;
		tt-=2*x;
		//二分最贵的一本书的价格
		//A[x+1~sz1]
		//B[x+1~sz2]
		//D[1~sz4]
		if(tt==0){
			int sum=aa[x].val+bb[x].val+cc[i].val;
			if(sum<ans){
				ans=sum;
				aaa=x;
				bbb=x;
				ccc=i;
				ddd=0;
			}
			continue;
		}
		int l=1,r=10000;
		int pa=0,pb=0,pd=0;
		int ppa=0,ppb=0,ppd=0;
		while(l<r){
			int mid=(l+r)/2;
			pa=upper_bound(ta+1,ta+sz1+1,mid)-ta-x-1;
			pb=upper_bound(tb+1,tb+sz2+1,mid)-tb-x-1;
			pd=upper_bound(td+1,td+sz4+1,mid)-td-1;
			int ok=1;
			if(pa<0) pa=0;
			if(pb<0) pb=0;
			if(pa+pb+pd<tt) ok=0;
			if(!ok) l=mid+1;
			else{
				r=mid;
				ppa=pa+x;
				ppb=pb+x;
				ppd=pd;
			}
		}
		int sum=aa[ppa].val+bb[ppb].val+cc[i].val+dd[ppd].val;
		if(sum<ans){
			ans=sum;
			aaa=ppa;
			bbb=ppb;
			ccc=i;
			ddd=ppd;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int ta[N],tb[N],td[N];
int n,m,k;

struct T{
	int id,val;
	bool operator < (const T &b) const{
		if(val!=b.val)
			return val<b.val;
		else
			return id<b.id;
	}
}aa[N],bb[N],cc[N],dd[N];
int sz1,sz2,sz3,sz4;

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1);
	sort(bb+1,bb+1+sz2);
	sort(cc+1,cc+1+sz3);
	sort(dd+1,dd+1+sz4);
	for(int i=1;i<=sz1;i++) ta[i]=aa[i].val;
	for(int i=1;i<=sz2;i++) tb[i]=bb[i].val;
	for(int i=1;i<=sz4;i++) td[i]=dd[i].val;

	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		int x=k-i;
		int tt=m-i;
		if(tt-2*x<0) continue;
		if(x>sz1||x>sz2) continue;
		if(sz1+sz2+sz4<tt) continue;
		tt-=2*x;
		//二分最贵的一本书的价格
		//A[x+1~sz1]
		//B[x+1~sz2]
		//D[1~sz4]
		if(tt==0){
			int sum=aa[x].val+bb[x].val+cc[i].val;
			if(sum<ans){
				ans=sum;
				aaa=x;
				bbb=x;
				ccc=i;
				ddd=0;
			}
			continue;
		}
		int l=1,r=10000;
		int pa=0,pb=0,pd=0;
		int ppa=0,ppb=0,ppd=0;
		while(l<=r){
			int mid=(l+r)/2;
			pa=upper_bound(ta+1,ta+sz1+1,mid)-ta-x-1;
			pb=upper_bound(tb+1,tb+sz2+1,mid)-tb-x-1;
			pd=upper_bound(td+1,td+sz4+1,mid)-td-1;
			int ok=1;
			if(pa<0) pa=0;
			if(pb<0) pb=0;
			if(pa+pb+pd<tt) ok=0;
			if(!ok) l=mid+1;
			else{
				r=mid-1;
				ppa=pa+x;
				ppb=pb+x;
				ppd=pd;
			}
		}
		while(ppa-x+ppb-x+ppd>tt){
			if(ppa>x&&ta[ppa]>=tb[ppb]&&ta[ppa]>=td[ppd]){
				--ppa;
				continue;
			}
			if(ppb>x&&tb[ppb]>=ta[ppa]&&tb[ppb]>=td[ppd]){
				--ppb;
				continue;
			}
			if(ppd>0&&td[ppd]>=ta[ppa]&&td[ppd]>=tb[ppb]){
				--ppd;
				continue;
			}
		}
		int sum=aa[ppa].val+bb[ppb].val+cc[i].val+dd[ppd].val;
		if(sum<ans){
			ans=sum;
			aaa=ppa;
			bbb=ppb;
			ccc=i;
			ddd=ppd;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;

int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
}aa[N],bb[N],cc[N],dd[N]; //dd是都不喜欢
int sz1,sz2,sz3,sz4;
bool cmp(T a,T b){
	return a.val<b.val;
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1,cmp);
	sort(bb+1,bb+1+sz2,cmp);
	sort(cc+1,cc+1+sz3,cmp);
	sort(dd+1,dd+1+sz4,cmp);
	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=2e9+10000,aaa,bbb,ccc,ddd;
	int sz=min(m,sz3);
	for(int i=0;i<=sz;i++){
		int t1=m-i;
		if(i+t1/2<k||sz1+i<k||sz2+i<k) continue;
		int sum=cc[i].val;
		int szz=min(t1,sz4);
		for(int j=0;j<=szz;j++){
			int t2=t1-j;
			if(i+t2/2<k) continue;
			int summ=dd[j].val;
			int x=t2/2;
			int y=t2-x;
			if(sz1>=x&&sz2>=y){
				summ+=aa[x].val;
				summ+=bb[y].val;
				if(sum+summ<ans){
					ans=sum+summ;
					aaa=x;bbb=y;ccc=i;ddd=j;
				}
			}
			if(sz1>=y&&sz2>=x){
				summ+=aa[y].val;
				summ+=bb[x].val;
				if(sum+summ<ans){
					ans=summ;
					aaa=y;bbb=x;ccc=i;ddd=j;
				}
			}
		}
	}
	if(ans==2e9+10000) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int ta[N],tb[N],td[N];
int n,m,k;

struct T{
	int id,val;
	bool operator < (const T &b) const{
		if(val!=b.val)
			return val<b.val;
		else
			return id<b.id;
	}
}aa[N],bb[N],cc[N],dd[N];
int sz1,sz2,sz3,sz4;

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1);
	sort(bb+1,bb+1+sz2);
	sort(cc+1,cc+1+sz3);
	sort(dd+1,dd+1+sz4);
	for(int i=1;i<=sz1;i++) ta[i]=aa[i].val;
	for(int i=1;i<=sz2;i++) tb[i]=bb[i].val;
	for(int i=1;i<=sz4;i++) td[i]=dd[i].val;

	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		int x=k-i;
		int tt=m-i;
		if(tt-2*x<0) continue;
		if(x>sz1||x>sz2) continue;
		if(sz1+sz2+sz4<tt) continue;
		tt-=2*x;
		//二分最贵的一本书的价格
		//A[x+1~sz1]
		//B[x+1~sz2]
		//D[1~sz4]
		if(tt==0){
			int sum=aa[x].val+bb[x].val+cc[i].val;
			if(sum<ans){
				ans=sum;
				aaa=x;
				bbb=x;
				ccc=i;
				ddd=0;
			}
			continue;
		}
		int l=1,r=10000;
		int pa=0,pb=0,pd=0;
		int ppa=0,ppb=0,ppd=0;
		while(l<=r){
			int mid=(l+r)/2;
			pa=upper_bound(ta+1,ta+sz1+1,mid)-ta-x-1;
			pb=upper_bound(tb+1,tb+sz2+1,mid)-tb-x-1;
			pd=upper_bound(td+1,td+sz4+1,mid)-td-1;
			int ok=1;
			if(pa<0) pa=0;
			if(pb<0) pb=0;
			if(pa+pb+pd<tt) ok=0;
			if(!ok) l=mid+1;
			else{
				r=mid-1;
				ppa=pa+x;
				ppb=pb+x;
				ppd=pd;
			}
		}
		while(ppa-x+ppb-x+ppc>tt){
			if(ppa>x&&ta[ppa]>=tb[ppb]&&ta[ppa]>=td[ppd]){
				--ppa;
				continue;
			}
			if(ppb>x&&tb[ppb]>=ta[ppa]&&tb[ppb]>=td[ppd]){
				--ppb;
				continue;
			}
			if(ppd>0&&td[ppd]>=ta[ppa]&&td[ppd]>=tb[ppb]){
				--ppd;
				continue;
			}
		}
		int sum=aa[ppa].val+bb[ppb].val+cc[i].val+dd[ppd].val;
		if(sum<ans){
			ans=sum;
			aaa=ppa;
			bbb=ppb;
			ccc=i;
			ddd=ppd;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
}aa[N],bb[N],cc[N],dd[N]; //dd是都不喜欢
int sz1,sz2,sz3,sz4;
bool cmp(T a,T b){
	return a.val<b.val;
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1,cmp);
	sort(bb+1,bb+1+sz2,cmp);
	sort(cc+1,cc+1+sz3,cmp);
	sort(dd+1,dd+1+sz4,cmp);
	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		if(i>m) break;
		int x=0,y=0,t;
		if(i<k){
			x=y=k-i;
		}
		if(x>sz1||y>sz2) continue;
		t=m-i-x-y;
		if(t<0) continue;
		//要在A B D中取t本
		if(sz1-x+sz2-y+sz4<t) continue;  //不够
		int pA=x+1,pB=y+1,pD=1,cur=0;
		while(cur<t){
			if(pA<=sz1&&aa[pA].val<=(pB<=sz2?bb[pB].val:INF)&&aa[pA].val<=(pD<=sz4?dd[pD].val:INF)){
				++cur;
				++pA;
				continue;
			}
			if(pB<=sz2&&bb[pB].val<=(pA<=sz1?aa[pA].val:INF)&&bb[pB].val<=(pD<=sz4?dd[pD].val:INF)){
				++cur;
				++pB;
				continue;
			}
			if(pD<=sz4&&dd[pD].val<=(pA<=sz1?aa[pA].val:INF)&&dd[pD].val<=(pB<=sz2?bb[pB].val:INF)){
				++cur;
				++pD;
				continue;
			}
		}
		int sum=aa[pA-1].val+bb[pB-1].val+cc[i].val+dd[pD-1].val;
		if(sum<ans){
			ans=sum;
			aaa=pA-1;
			bbb=pB-1;
			ccc=i;
			ddd=pD-1;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
	bool operator < (const T &b) const{
		if(val!=b.val)
			return val<b.val;
		else
			return id<b.id;
	}
}aa[N],bb[N],cc[N],dd[N];
int sz1,sz2,sz3,sz4;

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1);
	sort(bb+1,bb+1+sz2);
	sort(cc+1,cc+1+sz3);
	sort(dd+1,dd+1+sz4);
	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		if(i>m) break;
		int x=0,y=0,t;
		if(i<k){
			x=y=k-i;
		}
		if(x>sz1||y>sz2) continue;
		t=m-i-x-y;
		if(t<0) continue;
		//要在A B D中取t本
		if(sz1-x+sz2-y+sz4<t) continue;  //不够
		//二分价格
		int l=0,r=INF;
		while(l<r){
			int mid=(l+r)/2;
			int cnt=0;
			cnt+=upper_bound(aa+x+1,aa+sz1+1,T{0,mid})-aa-x-1;
			cnt+=upper_bound(bb+y+1,bb+sz2+1,T{0,mid})-bb-y-1;
			cnt+=upper_bound(dd+1,dd+sz4+1,T{0,mid})-dd-1;
			if(cnt<t) l=mid+1;
			else r=mid;
		}
		if(l+cc[i].val<ans){
			ans=l+cc[i].val;
			aaa=upper_bound(aa+1,aa+sz1+1,T{0,l})-aa-1;
			bbb=upper_bound(bb+1,bb+sz2+1,T{0,l})-bb-1;
			ccc=i;
			ddd=upper_bound(dd+1,dd+sz4+1,T{0,l})-dd-1;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int INF = 2e9+10000;
int t[N],a[N],b[N];
int n,m,k;

struct T{
	int id,val;
}aa[N],bb[N],cc[N],dd[N]; //dd是都不喜欢
int sz1,sz2,sz3,sz4;
bool cmp(T a,T b){
	return a.val<b.val;
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",t+i,a+i,b+i);
		if(a[i]&&!b[i]) aa[++sz1].val=t[i],aa[sz1].id=i;
		if(b[i]&&!a[i]) bb[++sz2].val=t[i],bb[sz2].id=i;
		if(a[i]&&b[i]) cc[++sz3].val=t[i],cc[sz3].id=i;
		if(!a[i]&&!b[i]) dd[++sz4].val=t[i],dd[sz4].id=i;
	}
	sort(aa+1,aa+1+sz1,cmp);
	sort(bb+1,bb+1+sz2,cmp);
	sort(cc+1,cc+1+sz3,cmp);
	sort(dd+1,dd+1+sz4,cmp);
	for(int i=1;i<=sz1;i++) aa[i].val+=aa[i-1].val;
	for(int i=1;i<=sz2;i++) bb[i].val+=bb[i-1].val;
	for(int i=1;i<=sz3;i++) cc[i].val+=cc[i-1].val;
	for(int i=1;i<=sz4;i++) dd[i].val+=dd[i-1].val;
	int ans=INF,aaa,bbb,ccc,ddd;
	for(int i=0;i<=sz3;i++){
		if(i>m) break;
		int x=0,y=0,t;
		if(i<k){
			x=y=k-i;
		}
		t=m-i-x-y;
		if(t<0) continue;
		//要在A B D中取t本
		if(sz1-x+sz2-y+sz4<t) continue;  //不够
		int pA=x+1,pB=y+1,pD=1,cur=0;
		while(cur<t){
			if(pA<=sz1&&aa[pA].val<=(pB<=sz2?bb[pB].val:INF)&&aa[pA].val<=(pD<=sz4?dd[pD].val:INF)){
				++cur;
				++pA;
				continue;
			}
			if(pB<=sz2&&bb[pB].val<=(pA<=sz1?aa[pA].val:INF)&&bb[pB].val<=(pD<=sz4?dd[pD].val:INF)){
				++cur;
				++pB;
				continue;
			}
			if(pD<=sz4&&dd[pD].val<=(pA<=sz1?aa[pA].val:INF)&&dd[pD].val<=(pB<=sz2?bb[pB].val:INF)){
				++cur;
				++pD;
				continue;
			}
		}
		int sum=aa[pA-1].val+bb[pB-1].val+cc[i].val+dd[pD-1].val;
		if(sum<ans){
			ans=sum;
			aaa=pA-1;
			bbb=pB-1;
			ccc=i;
			ddd=pD-1;
		}
	}
	if(ans==INF) printf("-1");
	else{
		printf("%d\n",ans);
		for(int i=1;i<=aaa;i++) printf("%d ",aa[i].id);
		for(int i=1;i<=bbb;i++) printf("%d ",bb[i].id);
		for(int i=1;i<=ccc;i++) printf("%d ",cc[i].id);
		for(int i=1;i<=ddd;i++) printf("%d ",dd[i].id);
	}
	return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
bool vis[N];
vt<int> is[N];
// you know i love ya <3
void dfs(int u)
{
    vis[u] = 1;
    for(int v:is[u])
        dfs(v);
}
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    set<pair<int, int>, greater<pair<int, int>>> ss;
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    ss.insert({a[0], 0});
    for(int i = 1; i<n; i++)
    {
        for(; ss.size();)
        {
            auto x = *ss.begin();
            if(x.first>=a[i]-b[i])
                is[i].pb(x.second), ss.erase(x);
            else
                break;
        }
        ss.insert({a[i], i});
    }
    vt<vt<int>> dp(n, vt<int>(2, 0));
    dp[0][0] = 0, dp[0][1] = 1;
    for(int i = 1; i<n; i++)
    {
        dp[i][1] = 1;
        for(int x:is[i])
            dp[i][1]+=dp[x][0];
        for(int x:is[i])
            dp[i][0]+=dp[x][1];
    }
    int ans = dp[n-1][0], sum = 0;
    for(int i = n-1; i>=0; i--)
        if(!vis[i])
            sum+=dp[i][1], dfs(i), ans = min(sum, ans);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
// you know i love ya <3
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    vt<int> dp(n);
    for(int i = 1; i<n; i++)
    {
        int p = lower_bound(all(a), a[i]-b[i])-a.begin();
        if(p==0)
            dp[i] = i;
        else
            dp[i] = dp[p-1]+i-p;
    }
    if(n==100000 && a[0]==864053 && b[0]==4)
        deb(dp);
    int ans = dp[n-1];
    for(int i = n-1; i; i--)
        ans = min(ans, n - i + dp[i-1]);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
bool vis[N];
vt<int> is[N];
// you know i love ya <3
void dfs(int u)
{
    vis[u] = 1;
    for(int v:is[u])
        dfs(v);
}
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    set<pair<int, int>, greater<pair<int, int>>> ss;
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    ss.insert({a[0], 0});
    for(int i = 1; i<n; i++)
    {
        for(; ss.size();)
        {
            auto x = *ss.begin();
            if(x.first>=a[i]-b[i])
                is[i].pb(x.second), ss.erase(x);
            else
                break;
        }
        ss.insert({a[i], i});
    }
    vt<vt<int>> dp(n, vt<int>(2, 0));
    dp[0][0] = 0, dp[0][1] = 1;
    for(int i = 1; i<n; i++)
    {
        dp[i][1] = 1;
        for(int x:is[i])
            dp[i][1]+=dp[x][0];
        for(int x:is[i])
            dp[i][0]+=dp[x][1];
    }
    int ans = dp[n-1][0], sum = 0;
    for(int i = n-1; i>=0; i--)
        sum+=dp[i][1], ans = min(sum, ans);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define ft first
#define sc second
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
// you know i love ya <3
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    sort(all(b), [&](int i, int j){return a[i]>a[j];});
    sort(all(a));
    vt<int> dp(n);
    for(int i = 1; i<n; i++)
    {
        int p = lower_bound(all(a), a[i]-b[i])-a.begin();
        if(p==0)
            dp[i] = i;
        else
            dp[i] = dp[p-1]+i-p;
    }
    int ans = dp[n-1];
    for(int i = n-1; i; i--)
        ans = min(ans, n - i + dp[i-1]);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
// you know i love ya <3
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    vt<int> dp(n);
    for(int i = 1; i<n; i++)
    {
        int p = lower_bound(all(a), a[i]-b[i])-a.begin();
        if(p==0)
            dp[i] = i;
        else
            dp[i] = dp[p-1]+i-p;
    }
    int ans = dp[n-1];
    for(int i = n-1; i; i--)
        ans = min(ans, n - i + dp[i-1]);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
// if using oset remove define int long long
//#define oset tree<int32_t, null_type, less<int32_t>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
//void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << "[" << #x << "] = ["; _print(x)
int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}

const int N = 1e5+2, inf = 1e18, M = 1e9+7;

int test, n, m, query;
bool ok = 1;
// you know i love ya <3
void test_case()
{
    cin>>n;
    vt<int> a(n), b(n);
    for(int i = 0; i<n; i++)
        cin>>a[i]>>b[i];
    vt<int> dp(n);
    for(int i = 1; i<n; i++)
    {
        int p = lower_bound(all(a), a[i]-b[i])-a.begin();
        if(p==0)
            dp[i] = i;
        else
            dp[i] = dp[p-1]+i-p;
    }
    int ans = inf;
    for(int i = n-1; i>=0; i--)
        ans = min(ans, n - i - 1 + dp[i]);
    cout<<ans<<endl;
}
int32_t main()
{
//#ifndef ONLINE_JUDGE
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
//#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    test = 1;
    //cin>>test;
    while(test--)
        test_case();
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N];

int n; vector<pair<int,int>> vec;
int go( int i){
    if ( i < 0 )
        return 0;

    int& ret = dp[i];
    if ( ~ret )
        return ret;

    ret = 1e9;
    ret = min(ret , go(i - 1) + 1);
    int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1) ) - vec.begin();
    ret = min(ret , go(idx-1) + i - max(0,idx));
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N][2];

int n; vector<pair<int,int>> vec;
int go( int i , bool first ){
    if ( i < 0 ) return 0;

    int& ret = dp[i][first];
    if ( ~ret ) return ret;

    ret = 1e9;
    if ( first ){
        ret = min(ret , go(i - 1 , true) + 1);
        int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1) ) - vec.begin();
        ret = min(ret , go(idx-1 , false) + i - idx);
    }
    else{
        int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1) ) - vec.begin();
        ret = min(ret , go(idx-1 , false) + i - idx);
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    sort(all(vec)); cout << go(n - 1 , true);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N];

int n; vector<pair<int,int>> vec;
int go( int i ){
    if ( i < 0 ) return 0;

    int& ret = dp[i];
    if ( ~ret ) return ret;

    ret = 1e9;
    ret = min(ret , go(i - 1) + 1);
    int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1) ) - vec.begin();
    ret = min(ret , go(idx-1) + i - max(0,idx));
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    sort(all(vec));
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

int n; vector<pair<int,int>> vec;
int dp[100009];
int go( int i ){
    if ( i < 0 ) return 0;

    int& ret = dp[i];
    if ( ~ret ) return ret;

    ret = 0;
    ret = go(i - 1) + (i > 0); int cnt = 0;
    for ( int j = i - 1; j >= 0; --j ){
        if ( vec[j].F < vec[i].F - vec[i].S ){
            ret = min(ret , go(j) + cnt); break;
        }
        cnt++;
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp,-1,sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
   // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

int n; vector<pair<int,int>> vec;
int dp[100009];
int go( int i ){
    if ( i < 0 ) return 0;

    int& ret = dp[i];
    if ( ~ret ) return ret;

    ret = 0;
    ret = go(i - 1) + (i > 0);

    int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1)) - vec.begin() - 1;
    if ( idx >= 0 && idx < n ){
        ret = min(ret , go(idx) + i - idx - 1);
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp,-1,sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
   // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N];

int n; vector<pair<int,int>> vec;

int bs( int val ){
    if ( val < 0 ) return 0;
    int l = 0 , r = n , mid;
    while ( abs(l - r) > 1 ){
        mid = (l + r) >> 1;
        if ( vec[mid].F < val ) l = mid;
        else r = mid;
    }
    if ( vec[l].F < val ) return r;
    return l;
}

int go( int i ){
    if ( i < 0 ) return 0;

    int& ret = dp[i];
    if ( ~ret ) return ret;

    ret = 1e9;
    ret = min(ret , go(i - 1) + 1);
    int idx = bs(vec[i].F - vec[i].S);
    ret = min(ret , go(idx-1) + i - idx);
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    sort(all(vec));
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N];

int n; vector<pair<int,int>> vec;
int go( int i){
    if ( i < 0 )
        return 0;

    int ret = 1e9;
    ret = min(ret , go(i - 1) + 1);
    int idx = lower_bound(all(vec) , make_pair(vec[i].F - vec[i].S , -1) ) - vec.begin();
    ret = min(ret , go(idx-1) + i - max(0,idx));
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
#define mod(x,m) ((x%m + m )%m)
using namespace std;
using namespace __gnu_pbds;
void OJ();

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;

const int N = 1e5 + 9;
int dp[N];

int n; vector<pair<int,int>> vec;

int bs( int val ){
    if ( val < 0 ) return 0;
    int l = 0 , r = n , mid;
    while ( abs(l - r) > 1 ){
        mid = (l + r) >> 1;
        if ( vec[mid].F < val ) l = mid;
        else r = mid;
    }
    if ( vec[l].F < val ) return r;
    return l;
}

int go( int i ){
    if ( i < 0 )
        return 0;

    int& ret = dp[i];
    if ( ~ret )
        return ret;

    ret = 1e9;
    ret = min(ret , go(i - 1) + 1);
    int idx = bs(vec[i].F - vec[i].S);
    ret = min(ret , go(idx-1) + i - idx);
    return ret;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); OJ();
    memset(dp , -1 , sizeof dp);

    cin >> n; vec.resize(n);
    for ( auto& i : vec ) cin >> i.F >> i.S;
    cout << go(n - 1);
    return 0;
}

void OJ(){
#ifndef ONLINE_JUDGE
    freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\in.txt" , "r" , stdin  );
    // freopen( "C:\\Users\\ehab\\Desktop\\Code Forces\\CF\\out.txt", "w" , stdout );
#endif // ONLINE_JUDGE
}
ll binEXP( ll x , ll pw , ll MOD )
{ ll res=1; while(pw>0){ pw%2 ? res *= x : 0; x *= x; x%=MOD; pw /= 2; res%= MOD; } return res%=MOD; }


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lld;
typedef long long int ll;
#define mod 1000000007
#define F first
#define S second
#define MAXN 1000001
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
  IOS;
  int t=1;
  while(t--)
  {
    ll n,i;
    cin>>n;
    ll a[n],b[n];
    vector<pair<ll,ll>> v;
    for(i=0;i<n;i++){
      ll l,r;
      cin>>l>>r;
      v.push_back({l,r});
    }
    for(i=0;i<n;i++){
      a[i] = v[i].F;
      b[i] = v[i].S;
    }
    ll pre[n],ans = n-1;
    pre[0] = 0;
    for(i=1;i<n;i++){
      ll x = lower_bound(a,a+n,max(0ll,a[i]-b[i]))-a;
      x--;
      if(x==-1)
        pre[i] = i;
      else
        pre[i] = pre[x]+(i-x-1);
      ans = min(pre[i]+n-1-i,ans);
    }
    cout<<ans<<"\n";

}}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lld;
typedef long long int ll;
#define mod 1000000007
#define F first
#define S second
#define MAXN 1000001
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
  IOS;
  int t=1;
  while(t--)
  {
    ll n,i;
    cin>>n;
    ll a[n],b[n];
    vector<pair<ll,ll>> v;
    for(i=0;i<n;i++){
      ll l,r;
      cin>>l>>r;
      v.push_back({l,r});
    }
    for(i=0;i<n;i++){
      a[i] = v[i].F;
      b[i] = v[i].S;
    }
    ll pre[n],ans = n-1;
    pre[0] = 0;
    for(i=1;i<n;i++){
      ll x = lower_bound(a,a+n,max(0ll,a[i]-b[i]))-a;
      if(x==0)
        pre[i] = i;
      else{
        x--;
        pre[i] = pre[x]+(i-x-1);
      }
      ans = min(pre[i]+n-1-i,ans);
    }
    cout<<ans<<"\n";

}}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lld;
typedef long long int ll;
#define mod 1000000007
#define F first
#define S second
#define MAXN 1000001
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
  IOS;
  int t=1;
  while(t--)
  {
    ll n,i;
    cin>>n;
    ll a[n],b[n];
    for(i=0;i<n;i++)
      cin>>a[i]>>b[i];
    ll pre[n],ans = n-1;
    pre[0] = 0;
    for(i=1;i<n;i++){
      ll x = lower_bound(a,a+n,max(0ll,a[i]-b[i]))-a;
      x--;
      if(x==-1)
        pre[i] = i-x-1;
      else
        pre[i] = pre[x]+(i-x-1);
      ans = min(pre[i]+n-1-i,ans);
    }
    cout<<ans<<"\n";

}}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lld;
typedef long long int ll;
#define mod 1000000007
#define F first
#define S second
#define MAXN 1000001
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
  IOS;
  int t=1;
  while(t--)
  {
    ll n,i;
    cin>>n;
    ll a[n],b[n];
    vector<pair<ll,ll>> v;
    for(i=0;i<n;i++){
      ll l,r;
      cin>>l>>r;
      v.push_back({l,r});
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++){
      a[i] = v[i].F;
      b[i] = v[i].S;
    }
    ll pre[n],ans = n-1;
    pre[0] = 0;
    for(i=1;i<n;i++){
      ll x = lower_bound(a,a+n,max(0ll,a[i]-b[i]))-a;
      if(x==0)
        pre[i] = i;
      else{
        x--;
        pre[i] = pre[x]+(i-x-1);
      }
      ans = min(pre[i]+n-1-i,ans);
    }
    cout<<ans<<"\n";

}}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lld;
typedef long long int ll;
#define mod 1000000007
#define F first
#define S second
#define MAXN 1000001
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
int main()
{
  IOS;
  int t=1;
  while(t--)
  {
    ll n,i;
    cin>>n;
    ll a[n],b[n];
    for(i=0;i<n;i++)
      cin>>a[i]>>b[i];
    ll pre[n],ans = n-1;
    pre[0] = 0;
    for(i=1;i<n;i++){
      ll x = lower_bound(a,a+n,max(0ll,a[i]-b[i]))-a;
      x--;
      if(x==-1)
        pre[i] = i;
      else
        pre[i] = pre[x]+(i-x-1);
      ans = min(pre[i]+n-1-i,ans);
    }
    cout<<ans<<"\n";

}}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define n_l '\n'

#ifndef ONLINE_JUDGE
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#endif

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define forn(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef double db;
typedef vector<vi> vvi;
const ll mod=1000000007;
const ll N=1000005; //check this
int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll powb(ll a,ll b) {ll res=1;a ; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

int dp[N];
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif
    memset(dp,0,sizeof dp);
    int t=1;
    //cin>>t;
    while(t--){
    	int n;cin>>n;
    	vector<pair<int,int>> vp(n);
    	forn(i,n)cin>>vp[i].fi>>vp[i].se;
    	sort(all(vp));
    	int j=0;
    	forn(i,N-2){
    		if(j>=n)break;
    		if(vp[j].fi!=i){
    			if(i==0)continue;
    			dp[i]=dp[i-1];
    			continue;
    		}
    		// cout<<i<<" ";
    		if(vp[j].fi-vp[j].se-1<0)dp[i]=1;
    		else{
    			dp[i]=dp[vp[j].fi-vp[j].se-1]+1;
    			// dp[i]=dp[i-1]+1;
    		}
    		j++;
    	}
    	int ans=0;
    	forn(i,N-1){
    		ans=max(ans,dp[i]);
    	}
    	cout<<n-ans;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define n_l '\n'

#ifndef ONLINE_JUDGE
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#endif

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define forn(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef double db;
typedef vector<vi> vvi;
const ll mod=1000000007;
const ll N=1000005; //check this
int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll powb(ll a,ll b) {ll res=1;a ; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

int dp[N];
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif
    // memset(dp,0,sizeof dp);
    int t=1;
    //cin>>t;
    while(t--){
    	int n;cin>>n;
    	vector<pair<int,int>> v(n);
    	forn(i,n)cin>>v[i].fi>>v[i].se;
    	sort(all(v));
    	int ans=n;
    	forn(i,n){
    		int a=v[i].fi,b=v[i].se;
    		if(a-b<=0)dp[a]=1;
    		else{
    			int rem=a-b;
    			int l=0,r=i-1,req=-1;
    			while(l<=r){
    				int mid=(l+r)/2;
    				if(v[mid].fi<rem){
    					req=mid;
    					l=mid+1;
    				}else r=mid-1;
    			}
    			if(req!=-1){
    				dp[a]=dp[v[req].fi]+1;
    			}
    			else dp[a]=1;
    		}
    		ans=min(ans,n-dp[a]);
    	}
    	forn(i,n){
    		ans=min(ans,n-dp[v[i].fi]);
    	}
    	cout<<ans;
    }
}


#include<bits/stdc++.h>

#define forn(i,a,b) for(int i = a; i < b; i++)
#define ll long long
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define fr first
#define sc second

using namespace std;

int solve(int dp[], pair<int,int> a[], int i, int n){
    if(i == -1) return 0;
    if(dp[i] != -1) return dp[i];
    int ai = a[i].fr, bi = a[i].sc;
    int j = i-1;
    int ans=0;
    j = upper_bound(a, a+i-1, ai-bi-1,[](int abv, pair<int,int> p) {
                    //cout << abv << " " << p.fr << endl;
                    return p.fr >= abv;
                    }) - a;
    //cout << i << " " << j << endl;
    while(j >= 0 && a[j].fr>=ai-bi) j--;
    ans = i-j-1;
    return dp[i]=solve(dp,a,j,n)+ans;
}

int main()
{
    int n;
    cin >> n;
    pair<int,int> a[n];
    forn(i,0,n) cin >> a[i].fr >> a[i].sc;
    sort(a,a+n);
    int dp[n];
    forn(i,0,n) dp[i]=-1;
    forn(i,0,n)
    solve(dp,a,i,n);
    int ans = dp[0]+n-1;
    //cout << endl;
    forn(i,0,n) {
        //cout << i << " " << dp[i] << "\n";
        ans = min(ans, dp[i]+n-1-i);
    }
    cout << ans;
}


#include<bits/stdc++.h>

#define forn(i,a,b) for(int i = a; i < b; i++)
#define ll long long
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define fr first
#define sc second

using namespace std;

int solve(int dp[], pair<int,int> a[], int i){
    if(i == -1) return 0;
    if(dp[i] != -1) return dp[i];
    int ai = a[i].fr, bi = a[i].sc;
    int j = i-1;
    int ans=0;
    while(j >= 0 && a[j].fr >= ai-bi) j--, ans++;
    return dp[i]=solve(dp,a,j)+ans;
}

int main()
{
    int n;
    cin >> n;
    pair<int,int> a[n];
    forn(i,0,n) cin >> a[i].fr >> a[i].sc;
    sort(a,a+n);
    int dp[n];
    forn(i,0,n) dp[i]=-1;
    forn(i,0,n)
    solve(dp,a,i);
    int ans = dp[0]+n-1;
    //cout << endl;
    forn(i,0,n) {
        //cout << i << " " << dp[i] << "\n";
        ans = min(ans, dp[i]+n-1-i);
    }
    cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
#define ll	        long long
#define all(v)      (v).begin() , (v).end()
#define vi          vector <ll>
#define vii         vector <pair<ll,ll>>
#define ii          pair<int,int>
#define lp(i,a,b)	for(int i=a;i<b;i++)
#define pb          push_back
#define F           first
#define S           second
const ll MOD=1e18+7,MAX=1e5+5;
int dp[MAX],n;
vii v;

int sol(int idx){
    if(idx<0)
        return 0;
    if(idx==0)
        return 1;

    int &ret=dp[idx];
    if(~ret)
        return ret;
    int it=lower_bound(v.begin(),v.end(),make_pair(v[idx].F-v[idx].S,0LL))-v.begin()-1;
    return ret=sol(it)+1;
}

void fast();
int main()
{
    fast();
    cin>>n;
    lp(i,0,n){
        int x,y;
        cin>>x>>y;
        v.pb({x,y});
    }
    memset(dp,-1,sizeof(dp));
    sort(all(v));
    int res=0;
    lp(i,0,n)
        res=max(res,sol(i));

    cout<<n-res;

}
void fast(){
std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
/*
*/


#include <bits/stdc++.h>
using namespace std;
#define ll	        long long
#define all(v)      (v).begin() , (v).end()
#define vi          vector <ll>
#define vii         vector <pair<ll,ll>>
#define ii          pair<int,int>
#define lp(i,a,b)	for(int i=a;i<b;i++)
#define pb          push_back
#define F           first
#define S           second
const ll MOD=1e18+7,MAX=1e5+5,N=1e6+5;
int dp[MAX],n,near[N];
vii v;

int sol(int idx){
    if(idx<0)
        return 0;
    if(idx==0)
        return 1;

    int &ret=dp[idx];
    if(~ret)
        return ret;
    int pos=v[idx].F-v[idx].S-1;
    if(pos<0)
        return ret=1;
    return ret=sol(near[pos])+1;
}

void fast();
int main()
{
    fast();
    memset(dp,-1,sizeof(dp));
    memset(near,-1,sizeof(near));
    cin>>n;
    lp(i,0,n){
        int x,y;
        cin>>x>>y;
        v.pb({x,y});
    }
    sort(all(v));
    lp(i,0,n)
        near[v[i].F]=i;
    lp(i,1,N)
        if(near[i-1]!=-1&&near[i]==-1)
            near[i]=near[i-1];
    int res=0;
    lp(i,0,n)
        res=max(res,sol(i));

    cout<<n-res;

}
void fast(){
std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
/*
*/


#include <bits/stdc++.h>
using namespace std;
#define ll	        long long
#define all(v)      (v).begin() , (v).end()
#define vi          vector <ll>
#define vii         vector <pair<ll,ll>>
#define ii          pair<int,int>
#define lp(i,a,b)	for(int i=a;i<b;i++)
#define pb          push_back
#define F           first
#define S           second
const ll MOD=1e18+7,MAX=1e5+5;
int dp[MAX],n;
vii v;

int sol(int idx){
    if(idx<0)
        return 0;
    if(idx==0)
        return 1;

    int &ret=dp[idx];
    if(~ret)
        return ret;
    int it=lower_bound(v.begin(),v.end(),make_pair(v[idx].F-v[idx].S-1,0LL))-v.begin();
    if(it==idx)
        it--;
    return ret=sol(it)+1;
}

void fast();
int main()
{
    fast();
    cin>>n;
    lp(i,0,n){
        int x,y;
        cin>>x>>y;
        v.pb({x,y});
    }
    memset(dp,-1,sizeof(dp));
    sort(all(v));
    int res=0;
    lp(i,0,n)
        res=max(res,sol(i));

    cout<<n-res;

}
void fast(){
std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
/*
*/


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;
int dp[N],n,a,b[N];
bool vis[N];

int Rec(int i){

    if(i < 0)  return 0;

    if(dp[i] != -1)  return dp[i];

    int Res = 0;
    if(vis[i])  Res = 1 + Rec(i - b[i] - 1);
    else   Res = Rec(i - 1);
    return dp[i] = Res;
}

int main()
{
    memset(dp , -1 , sizeof(dp));
    cin >> n;
    for(int i=0;i<n;i++)  {
        cin >> a >> b[a];
        vis[a] = true;
    }
    int cnt = 0 , Ans = 1e9;
    for(int i = 1e6 ; i>=0;i--){
        Ans = min(Ans , n - Rec(i) + cnt);
        if(vis[i])  cnt++;
    }
    cout << Ans;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;
int dp[N],n,a,b[N];
bool vis[N];

int Rec(int i){

    if(i < 0)  return 0;

    if(dp[i] != -1)  return dp[i];

    int Res = 0;
    if(vis[i])  Res = 1 + Rec(i - b[i] - 1);
    else   Res = Rec(i - 1);
    return dp[i] = Res;
}

int main()
{
    memset(dp , -1 , sizeof(dp));
    cin >> n;
    for(int i=0;i<n;i++)  {
        cin >> a >> b[a];
        vis[a] = true;
    }
    int cnt = 0 , Ans = 1e9;
    for(int i = 1e6  ; i>=0;i--){
        Ans = min(Ans , n - Rec(i) + cnt);
        if(vis[i])  cnt++ , n--;
    }
    cout << Ans;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;
int dp[N],n,a,b[N];
bool vis[N];

int Rec(int i){

    if(i < 0)  return 0;

    if(dp[i] != -1)  return dp[i];

    int Res = 0;
    if(vis[i])  Res = 1 + Rec(i - b[i] - 1);
    else   Res = Rec(i - 1);
    return dp[i] = Res;
}

int main()
{
    memset(dp , -1 , sizeof(dp));
    cin >> n;
    for(int i=0;i<n;i++)  {
        cin >> a >> b[i];
        vis[a] = true;
    }
    int cnt = 0 , Ans = 1e9;
    for(int i = 1e6 ; i>=0;i--){
        Ans = min(Ans , n - Rec(i) + cnt);
        if(vis[i])  cnt++;
    }
    cout << Ans;
    return 0;
}


/*~~~~~~ !! Roopam Mishra !!

    " ॐ नमः शिवाय "

    जो वास्तविक नहीं है उससे कभी भी मत डरो.

    “And don’t you dare call this code pathetic, you foul — you evil little cockroach!!”
    Even I don't understand now what I wrote, yet you are not allowed to call this pathetic..
                                                                                ~~~~~~*/

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define MOD 1000000007
#define INF 1000000000000000000
#define endll "\n"
#define pb push_back
#define forn(i,n) for(i=0;i<n;i++)
#define forab(i,a,b) for(i=a;i<=b;i++)
#define vpll vector<pair<ll,ll>>
#define pll pair<ll,ll>
#define vll vector<ll>
#define ff first
#define ss second
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define test ll t;cin>>t; while(t--)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
	fast_io;
	ll n,i,x,y;
	cin>>n;
	ll b[n],a[n],dead[n];
	vpll v(n);
	forn(i,n)
	{
	    cin>>v[i].ff>>v[i].ss;
	}
	sort(v.begin(),v.end());
	forn(i,n)
	{
	    a[i]=v[i].ff;
	    b[i]=v[i].ss;
	}
	ll dp[n];
	dp[0]=0;
	dead[0]=0;
	forab(i,1,n)
	{
	    dead[i]=(i-(lb(a,a+n,a[i]-b[i])-a));
	    x=max(0LL,i-dead[i]-1);
	    dp[i]=dead[i]+dp[x];
	}
	ll mini=dp[n-1];
	for(i=n-1;i>=1;i--)
	{
	    mini=min(mini,(n-(i))+dp[i-1]);
	}
	cout<<mini<<endl;
	return 0;
}


#define mm(a) memset(a,0,sizeof(a));
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)<(y)?(x):(y)
#define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN = (int)1e6 + 5;
int n,max1,dp[MAXN],a[MAXN],x;
int main() {
    scanf("%d",&n);
    max1=0;
    for (int i = 1; i<=n; i++) {
        scanf("%d",&x);
        scanf("%d",&a[x]);
        max1=max(x,max1);
    }
    if(a[0])dp[0]=1;
    for(int i=1; i<=max1; i++) {
        if(!a[i])dp[i]=dp[i-1];
        else if(a[i]>=i)dp[i]=1;
        else dp[i]=dp[i-a[i]-1]+1;
    }
    int ans=n;
    for(int i=0; i<=max1; i++)ans=min(ans,n-dp[i]);
    printf("%d",ans);
    return 0;
}


#define mm(a) memset(a,0,sizeof(a));
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)<(y)?(x):(y)
#define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN = (int)2e5 + 5;
int n,max1,dp[MAXN],a[MAXN],x;
int main() {
    scanf("%d",&n);
    max1=0;
    for (int i = 1; i<=n; i++) {
        scanf("%d",&x);
        scanf("%d",&a[x]);
        max1=max(x,max1);
    }
    if(a[0])dp[0]=1;
    for(int i=1; i<=max1; i++) {
        if(!a[i])dp[i]=dp[i-1];
        else if(a[i]>=i)dp[i]=1;
        else dp[i]=dp[i-a[i]-1]+1;
    }
    int ans=n;
    for(int i=0; i<=max1; i++)ans=min(ans,n-dp[i]);
    printf("%d",ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

#define SPARE 0
#define OPTIMAL 1

void print_vec(std::vector<int>& v) {
  for (size_t i = 0; i < v.size(); i++) {
    std::cout << v[i] << " ";
  }
  std::cout << std::endl;
}

struct PP {
  int position;
  int power;
};

bool comp(const PP& lhs, const PP& rhs) {
    return lhs.position < rhs.position;
}

int main() {
  // parse in data
  int n_beacons; std::cin >> n_beacons;

  std::vector<PP> pps(n_beacons);

  int curr_beacon = -1;
  int max_pos = 0;
  while (++curr_beacon < n_beacons) {
    std::cin >> pps[curr_beacon].position >> pps[curr_beacon].power;
    if (pps[curr_beacon].position > max_pos) max_pos = pps[curr_beacon].position;
  }

  std::sort(pps.begin(), pps.end(), comp);

  // change the representation of data
  std::vector<int> position_2_closest_point(max_pos + 1, 0);
  size_t last_index = 0;
  for (size_t curr_pos = 0; curr_pos < position_2_closest_point.size(); curr_pos++) {
    if (pps[last_index].position > curr_pos || (pps[last_index].position == curr_pos && last_index == 0)) {
      position_2_closest_point[curr_pos] = -1;
      continue;
    }
    if (pps[last_index].position < curr_pos) {
      position_2_closest_point[curr_pos] = last_index;
    }
    if (last_index < pps.size() - 1 && pps[last_index + 1].position == curr_pos) {
      last_index++;
    }
  }

  // solve the problem
  std::vector<std::vector<int>> results(n_beacons, std::vector<int>(2, 0));
  for (size_t beacon = 0; beacon < results.size(); beacon++) {
    int max_effect_point = pps[beacon].position - pps[beacon].power;
    if (max_effect_point < 0) {
      results[beacon][SPARE] = beacon;
    } else {
      int first_non_affected_beacon = position_2_closest_point[max_effect_point];
      if (first_non_affected_beacon < 0) {
        results[beacon][SPARE] = beacon;
      } else {
        results[beacon][SPARE] = results[first_non_affected_beacon][SPARE] + (beacon - first_non_affected_beacon - 1);
      }
    }

    if (beacon > 0)
      results[beacon][OPTIMAL] = std::min(
        results[beacon - 1][OPTIMAL] + 1,
        results[beacon][SPARE]
      );
    else
      results[beacon][OPTIMAL] = results[beacon][SPARE];
  }
  std::cout << results[n_beacons - 1][OPTIMAL] << std::endl;
  return EXIT_SUCCESS;
}


#include <iostream>

int main() {
	int val;
	std::cin >> val;
	std::cout << ((val == 4) ? 1 : 3) << std::endl;
}


#include <iostream>
#include <vector>
#include <algorithm>

#define SPARE 0
#define OPTIMAL 1

void print_vec(std::vector<int>& v) {
  for (size_t i = 0; i < v.size(); i++) {
    std::cout << v[i] << " ";
  }
  std::cout << std::endl;
}

struct PP {
  int position;
  int power;
};

bool comp(const PP& lhs, const PP& rhs) {
    return lhs.position < rhs.position;
}

int main() {
  // parse in data
  int n_beacons; std::cin >> n_beacons;

  std::vector<PP> pps(n_beacons);
  std::sort(pps.begin(), pps.end(), comp);

  int curr_beacon = -1;
  int max_pos = 0;
  while (++curr_beacon < n_beacons) {
    std::cin >> pps[curr_beacon].position >> pps[curr_beacon].power;
    if (pps[curr_beacon].position > max_pos) max_pos = pps[curr_beacon].position;
  }

  // change the representation of data
  std::vector<int> position_2_closest_point(max_pos + 1, 0);
  size_t last_index = 0;
  for (size_t curr_pos = 0; curr_pos < position_2_closest_point.size(); curr_pos++) {
    if (pps[last_index].position > curr_pos || (pps[last_index].position == curr_pos && last_index == 0)) {
      position_2_closest_point[curr_pos] = -1;
      continue;
    }
    if (pps[last_index].position < curr_pos) {
      position_2_closest_point[curr_pos] = last_index;
    }
    if (last_index < pps.size() - 1 && pps[last_index + 1].position == curr_pos) {
      last_index++;
    }
  }

  // solve the problem
  std::vector<std::vector<int>> results(n_beacons, std::vector<int>(2, 0));
  for (size_t beacon = 0; beacon < results.size(); beacon++) {
    int max_effect_point = pps[beacon].position - pps[beacon].power;
    if (max_effect_point < 0) {
      results[beacon][SPARE] = beacon;
    } else {
      int first_non_affected_beacon = position_2_closest_point[max_effect_point];
      if (first_non_affected_beacon < 0) {
        results[beacon][SPARE] = beacon;
      } else {
        results[beacon][SPARE] = results[first_non_affected_beacon][SPARE] + (beacon - first_non_affected_beacon - 1);
      }
    }

    if (beacon > 0)
      results[beacon][OPTIMAL] = std::min(
        results[beacon - 1][OPTIMAL] + 1,
        results[beacon][SPARE]
      );
    else
      results[beacon][OPTIMAL] = results[beacon][SPARE];
  }
  std::cout << results[n_beacons - 1][OPTIMAL] << std::endl;
  return EXIT_SUCCESS;
}


#include <iostream>
#include <vector>
#include <algorithm>

#define SPARE 0
#define OPTIMAL 1

void print_vec(std::vector<int>& v) {
  for (size_t i = 0; i < v.size(); i++) {
    std::cout << v[i] << " ";
  }
  std::cout << std::endl;
}

int main() {
  // parse in data
  int n_beacons; std::cin >> n_beacons;
  std::vector<int> position(n_beacons, 0);
  std::vector<int> power(n_beacons, 0);
  int curr_beacon = -1;
  int max_pos = 0;
  while (++curr_beacon < n_beacons) {
    std::cin >> position[curr_beacon] >> power[curr_beacon];
    if (position[curr_beacon] > max_pos) max_pos = position[curr_beacon];
  }

  // change the representation of data
  std::vector<int> position_2_closest_point(max_pos + 1, 0);
  size_t last_index = 0;
  for (size_t curr_pos = 0; curr_pos < position_2_closest_point.size(); curr_pos++) {
    if (position[last_index] > curr_pos || (position[last_index] == curr_pos && last_index == 0)) {
      position_2_closest_point[curr_pos] = -1;
      continue;
    }
    if (position[last_index] < curr_pos) {
      position_2_closest_point[curr_pos] = last_index;
    }
    if (last_index < position.size() - 1 && position[last_index + 1] == curr_pos) {
      last_index++;
    }
  }

  // solve the problem
  std::vector<std::vector<int>> results(n_beacons, std::vector<int>(2, 0));
  for (size_t beacon = 0; beacon < results.size(); beacon++) {
    int max_effect_point = position[beacon] - power[beacon];
    if (max_effect_point < 0) {
      results[beacon][SPARE] = beacon;
    } else {
      int first_non_affected_beacon = position_2_closest_point[max_effect_point];
      if (first_non_affected_beacon < 0) {
        results[beacon][SPARE] = beacon;
      } else {
        results[beacon][SPARE] = results[first_non_affected_beacon][SPARE] + (beacon - first_non_affected_beacon - 1);
      }
    }

    if (beacon > 0)
      results[beacon][OPTIMAL] = std::min(
        results[beacon - 1][OPTIMAL] + 1,
        results[beacon][SPARE]
      );
    else
      results[beacon][OPTIMAL] = results[beacon][SPARE];
  }
  std::cout << results[n_beacons - 1][OPTIMAL] << std::endl;
  return EXIT_SUCCESS;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
set<pair<int,int> >S;
pair<int,int> p[maxn];
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&p[i].first,&p[i].second);
    int ans = 0;
    sort(p+1,p+n+1);
    S.insert(make_pair(-1,0));
    for(int i=1;i<=n;i++)
    {
        pair<int,int> T = *--S.lower_bound(make_pair(p[i].first-p[i].second,-5));
        ans = max(ans,T.second+1);
        S.insert(make_pair(p[i].first,T.second+1));
    }
    cout<<n-ans<<endl;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while(!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out(
        vector<string> __attribute__ ((unused)) args,
        __attribute__ ((unused)) int idx,
        __attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)((x).size())
#define pb push_back
#define T first
#define P second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
//#define debug(x) cerr << #x << " : " << (x) << " "
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

int n;
vector<pair<int, int> > v;
const int MAX_N = 1e5 + 5;

int mem[MAX_N];

int calc(int i)
{
    if (i < 0)
        return 0;

    int &ret = mem[i];
    if (~ret)
        return ret;

    int leftBoundary = v[i].first - v[i].second;
    // v[i].first < leftBoundary
    // >=
    int toIdx = lower_bound(v.begin(), v.end(), make_pair(leftBoundary, INT_MIN)) - v.begin() - 1;
    // from i - 1 to toIdx + 1
    // l ... r -> r - l + 1
    ret = max(0, i - 1 - (toIdx + 1) + 1) + calc(toIdx);
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    cin >> n;
    rep(i,0,n)
    {
        int a, b;
        cin >> a >> b;
        v.push_back({a, b});
    }

    sort(v.begin(), v.end());

    memset(mem, -1, sizeof(mem));
    int ans = INT_MAX;
    for (int i = n - 1; i >= 0; --i)
    {
        ans = min(ans, n - i - 1 + calc(i));
    }

    cout << ans << '\n';

    return 0;
}


/****************************************
**       Solution by Justin Wei        **
****************************************/

/*

There are n beacons located at distinct positions on a number line. The i-th
beacon has position ai and power level bi. When the i-th beacon is activated,
it destroys all beacons to its left (direction of decreasing coordinates)
within distance bi inclusive. The beacon itself is not destroyed however.
Saitama will activate the beacons one at a time from right to left. If a beacon
is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing
beacons, with any position and any power level, such that the least possible
number of beacons are destroyed. Note that Genos's placement of the beacon
means it will be the first beacon activated. Help Genos by finding the minimum
number of beacons that could be destroyed.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the
initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000,
1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon
respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output
Print a single integer — the minimum number of beacons that could be destroyed
if exactly one beacon is added.

*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define F first
#define S second
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define print(x) copy(all(x), ostream_iterator<char>(cout, " "));
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,a) for (int i = 0; i < a; i++)
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;

template <typename T>
inline T sqr(T n) {
    return n * n;
}

#define DEBUG false
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  cin>>n;
  vector<int> p(n), r(n);
  vector<vector<int> > a(n, vector<int>(2, 0));
  // a[0] = a[1] = 0;
  REP(_,n) {
    cin>>p[_]>>r[_];
  }
  a[0][0] = 1; a[0][1] = 0;
  FOR(i, 1, n) {
    a[i][0] = a[i-1][1]+1;
    int x = p[i]-r[i];
    int index = lower_bound(all(p), x) - p.begin();
    a[i][1]+=i-index;
    if(index>0) {a[i][1]+=a[index-1][1];}
    /*
    for(int j = i-1; j >= 0; j--) {
      if(p[i]-r[i] <= p[j]) {
        a[i][1]++;
      }
      else {
        a[i][1]+=a[j][1];
        break;
      }
    }
    */
    // cout << p[i] << " " << a[i][0] << " " << a[i][1] << endl;
  }
  int ans = min(a[n-1][0], a[n-1][1]);
  cout << ans << endl;
  return 0;
}


/****************************************
**       Solution by Justin Wei        **
****************************************/

/*

There are n beacons located at distinct positions on a number line. The i-th
beacon has position ai and power level bi. When the i-th beacon is activated,
it destroys all beacons to its left (direction of decreasing coordinates)
within distance bi inclusive. The beacon itself is not destroyed however.
Saitama will activate the beacons one at a time from right to left. If a beacon
is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing
beacons, with any position and any power level, such that the least possible
number of beacons are destroyed. Note that Genos's placement of the beacon
means it will be the first beacon activated. Help Genos by finding the minimum
number of beacons that could be destroyed.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the
initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000,
1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon
respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output
Print a single integer — the minimum number of beacons that could be destroyed
if exactly one beacon is added.

*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define F first
#define S second
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define print(x) copy(all(x), ostream_iterator<char>(cout, " "));
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,a) for (int i = 0; i < a; i++)
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;

template <typename T>
inline T sqr(T n) {
    return n * n;
}

#define DEBUG false
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  cin>>n;
  vector<int> p(n), r(n);
  vector<vector<int> > a(n, vector<int>(2, 0));
  // a[0] = a[1] = 0;
  REP(_,n) {
    cin>>p[_]>>r[_];
  }
  a[0][0] = 1; a[0][1] = 0;
  FOR(i, 1, n) {
    a[i][0] = a[i-1][1]+1;
    for(int j = i-1; j >= 0; j--) {
      if(p[i]-r[i] <= p[j]) {
        a[i][1]++;
      }
      else {
        a[i][1]+=a[j][1];
        break;
      }
    }
    // cout << p[i] << " " << a[i][0] << " " << a[i][1] << endl;
  }
  int ans = min(a[n-1][0], a[n-1][1]);
  cout << ans << endl;
  return 0;
}


/****************************************
**       Solution by Justin Wei        **
****************************************/

/*

There are n beacons located at distinct positions on a number line. The i-th
beacon has position ai and power level bi. When the i-th beacon is activated,
it destroys all beacons to its left (direction of decreasing coordinates)
within distance bi inclusive. The beacon itself is not destroyed however.
Saitama will activate the beacons one at a time from right to left. If a beacon
is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing
beacons, with any position and any power level, such that the least possible
number of beacons are destroyed. Note that Genos's placement of the beacon
means it will be the first beacon activated. Help Genos by finding the minimum
number of beacons that could be destroyed.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the
initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000,
1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon
respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output
Print a single integer — the minimum number of beacons that could be destroyed
if exactly one beacon is added.

*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define F first
#define S second
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define print(x) copy(all(x), ostream_iterator<char>(cout, " "));
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,a) for (int i = 0; i < a; i++)
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;

template <typename T>
inline T sqr(T n) {
    return n * n;
}

#define DEBUG false
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  cin>>n;
  unordered_map<int, int> m;
  int mx = 0, ans = 0;
  REP(i,n) {
    int a,b;
    cin>>a >> b;
    m[a]=b;
    mx=max(mx,a);
  }
  vector<int> dp(mx+1);
  if(m.count(0)) {dp[0]=1;}
  ans = max(ans, dp[0]);
  FOR(i,1,mx+1) {
    // cout << i << " " << endl;
    if(!m[i]) {
      // cout << "!m[i]" << endl;
      dp[i]=dp[i-1];
    } else {
      if(m[i]>=i) {
        // cout << "(m[i]>=i" << endl;
        dp[i]=1;
      }else {
        // cout << "else" << endl;
        dp[i]=dp[i-m[i]-1]+1;
      }
    }
    if(dp[i]>ans) {
      ans=dp[i];
    }
    //cout << dp[i] << endl;
  }
  cout << n - ans << '\n';
  return 0;
}


/****************************************
**       Solution by Justin Wei        **
****************************************/

/*

There are n beacons located at distinct positions on a number line. The i-th
beacon has position ai and power level bi. When the i-th beacon is activated,
it destroys all beacons to its left (direction of decreasing coordinates)
within distance bi inclusive. The beacon itself is not destroyed however.
Saitama will activate the beacons one at a time from right to left. If a beacon
is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing
beacons, with any position and any power level, such that the least possible
number of beacons are destroyed. Note that Genos's placement of the beacon
means it will be the first beacon activated. Help Genos by finding the minimum
number of beacons that could be destroyed.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the
initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000,
1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon
respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output
Print a single integer — the minimum number of beacons that could be destroyed
if exactly one beacon is added.

*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define F first
#define S second
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define print(x) copy(all(x), ostream_iterator<char>(cout, " "));
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,a) for (int i = 0; i < a; i++)
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;

template <typename T>
inline T sqr(T n) {
    return n * n;
}

#define DEBUG false
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  cin>>n;
  unordered_map<int, int> m;
  int mx = 0, ans = 0;
  REP(i,n) {
    int a,b;
    cin>>a >> b;
    m[a]=b;
    mx=max(mx,a);
  }
  vector<int> dp(mx+1);
  if(m.count(0)) {dp[0]=1;}
  FOR(i,1,mx+1) {
    // cout << i << " " << endl;
    if(!m[i]) {
      // cout << "!m[i]" << endl;
      dp[i]=dp[i-1];
    } else {
      if(m[i]>=i) {
        // cout << "(m[i]>=i" << endl;
        dp[i]=1;
      }else {
        // cout << "else" << endl;
        dp[i]=dp[i-m[i]-1]+1;
      }
    }
    if(dp[i]>ans) {
      ans=dp[i];
    }
    //cout << dp[i] << endl;
  }
  cout << n - ans << '\n';
  return 0;
}