/****************************************
**       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) {
    // 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;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

 int dp[1000005];
    int  Power[1000005];

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    if(Power[0]!=0)dp[0]=1;

    lp(1,max_number+1){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number);

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int dp[n];
    int  Power[1000005];
    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    if(Power[0]!=0)dp[0]=1;

    for(int i=1;i<=max_number;i++){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number+1);

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

 int dp[1000005];
    int  Power[1000005];

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    if(dp[0]!=0)dp[0]=1;

    for(int i=1;i<=max_number;i++){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number+1);

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int dp[n];

    vector<pair<int,int> >arr;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    arr.push_back({a,b});
    }
    sort(all(arr));

    dp[0]=0;

    int flag=arr[0].first;
    int ans=0;

    lp(1,n){

        if((arr[i].first-arr[i].second)>flag){

            flag=arr[i].first;
        }
        else{
            ans++;
        }

    }

    cout<<ans;

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

 int dp[1000005];
    int  Power[1000005];

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    if(Power[0]!=0)dp[0]=1;

    for(int i=1;i<=max_number;i++){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number+1);

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int dp[n];

    vector<pair<int,int> >arr;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    arr.push_back({a,b});
    }
    sort(all(arr));

    dp[0]=0;

    int flag=arr[0].first;
    int ans=0;

    lp(1,n){

        if(abs(arr[i].first-arr[i].second)>flag){

            flag=arr[i].first;
        }
        else{
            ans++;
        }

    }

    cout<<ans;

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

int dp[1000005];
int  Power[1000005];

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    for(int i=1;i<=max_number;i++){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number+1);

    return 0;

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<list>
#include <algorithm>
#include<vector>
#include<set>
#include <cctype>
#include <cstring>
#include <cstdio>
#include<queue>
#include<stack>
#include<bitset>
#include<time.h>
#include<fstream>

/******************************************************/

using namespace std;

/********************define***************************/
#define ll 				long long
#define ld				long double
#define all(v)				((v).begin()), ((v).end())
#define for_(vec) 		for(int i=0;i<(int)vec.size();i++)
#define lp(j,n)		for(int i=j;i<(int)(n);i++)
#define clr(arr,x)  memset(arr,x,sizeof(arr))
#define fillstl(arr,X)    fill(arr.begin(),arr.end(),X)
#define pb         push_back
#define mp	   		make_pair
#define print_vector(X)  for(int i=0;i<X.size();i++)
/********************************************************/
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
/***********************function************************/

void fast()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
}
void online_judge()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
const int flag_max=0x3f3f3f3f;
const ll OO=1e9+9 ;
const double EPS = (1e-7);
int dcmp(double x, double y)
{
    return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;
}
ll gcd(ll a, ll b)
{
    return !b ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

/***********************main_problem****************************/

 int dp[1000005];
    int  Power[1000005];

/**************************Bisho_O*****************************************/
int main()
{

    fast();

    int n;
    cin>>n;

    int max_number=0;

    lp(0,n){
    int a,b;
    cin>>a>>b;
    Power[a]=b;
    max_number=max(max_number,a);
    }

    dp[0]=0;

    if(Power[0]!=0)dp[0]=1;

    lp(1,max_number+1){

        if(Power[i]==0){

            dp[i]=dp[i-1];
        }
        else{

            dp[i]=(Power[i]>=i?1: dp[i-Power[i]-1]+1 );

        }

    }

    cout<<n-*max_element(dp,dp+max_number+1);

    return 0;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1000006];
ll dp[1000006];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    for(int i = 0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        a[x] =  y;
    }
    if(a[0] >0 ) dp[0] = 1;
    ll ans = 0;
    for(int i = 0;i<=1e6;i++)
    {
        if(a[i])
        {
            if(a[i] >= i)
            {
                dp[i] = 1;
            }
            else
            {
                dp[i] = dp[i - a[i] -1]+1;
            }
        }
        else
        {
            dp[i] = dp[i-1];
        }
        ans = max(ans,dp[i]);
    }
    cout<< n - ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1000006];
ll dp[1000006];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    for(int i = 0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        a[x] =  y;
    }
    if(a[0] >0 ) dp[0] = 1;
    ll ans = 0;
    for(int i = 1;i<=1e6;i++)
    {
        if(a[i])
        {
            if(a[i] >= i)
            {
                dp[i] = 1;
            }
            else
            {
                dp[i] = dp[i - a[i] -1]+1;
            }
        }
        else
        {
            dp[i] = dp[i-1];
        }
        ans = max(ans,dp[i]);
    }
    cout<< n - ans<<endl;
    return 0;
}


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

using namespace std;

vector<int> findFirstNonOverlapping(vector<pair<int, int> > &beacons, int numBeacons) {
    vector<int> indexNonOverlapping(numBeacons);
    fill_n(indexNonOverlapping.begin(), numBeacons, -1);

    for (int i = numBeacons - 1; i >= 0; i--) {
        pair<int, int> start = beacons[i];
        int range = start.first - start.second;
        range = range < 0 ? 0: range;

        for (int j = i-1; j >= 0; j--) {
            pair<int, int> stop = beacons[j];
            if (stop.first < range) {
                // we've found it
                indexNonOverlapping[i] = j;
                break;
            }
        }
    }
    return indexNonOverlapping;
}

void solve(vector<pair<int, int> > &beacons, int numBeacons) {
    sort(beacons.begin(), beacons.end());
    vector<int> nonOverlapping = findFirstNonOverlapping(beacons, numBeacons);

    vector<int> dp(numBeacons); // represents the minimum number of beacons destroyed at any given points
    dp[0] = 0;
    for (int i = 1; i < numBeacons; i++) {
        if (nonOverlapping[i] == -1) {
            // if this one is used, it will destroy all to the left
            dp[i] = i;
        } else {
            int overlapping = i - nonOverlapping[i] - 1; // gets the distance between them
            dp[i] = overlapping + dp[nonOverlapping[i]];
        }
    }

    int ans = dp[0] + numBeacons - 1;
    for (int i = 1; i < numBeacons; i++) {
        ans = min(dp[i] + (numBeacons - i - 1), ans);
    }
    cout << ans;
}

int main(int argc, char** argv) {
    int numBeacons;
    cin >> numBeacons;

    vector<pair<int, int> > beacons(numBeacons);

    for (int i = 0; i < numBeacons; i++) {
        int position, power;
        cin >> position >> power;
        beacons[i] = make_pair(position, power);
    }
    solve(beacons, numBeacons);
}


#include <iostream>
#include <vector>

using namespace std;

vector<int> findFirstNonOverlapping(vector<pair<int, int> > &beacons, int numBeacons) {
    vector<int> indexNonOverlapping(numBeacons);
    fill_n(indexNonOverlapping.begin(), numBeacons, -1);

    for (int i = numBeacons - 1; i >= 0; i--) {
        pair<int, int> start = beacons[i];
        int range = start.first - start.second;
        range = range < 0 ? 0: range;

        for (int j = i-1; j >= 0; j--) {
            pair<int, int> stop = beacons[j];
            if (stop.first < range) {
                // we've found it
                indexNonOverlapping[i] = j;
                break;
            }
        }
    }
    return indexNonOverlapping;
}

void solve(vector<pair<int, int> > &beacons, int numBeacons) {
    std::sort(beacons.begin(), beacons.end());
    vector<int> nonOverlapping = findFirstNonOverlapping(beacons, numBeacons);

    vector<int> dp(numBeacons); // represents the minimum number of beacons destroyed at any given points
    dp[0] = 0;
    for (int i = 1; i < numBeacons; i++) {
        if (nonOverlapping[i] == -1) {
            // if this one is used, it will destroy all to the left
            dp[i] = i;
        } else {
            int overlapping = i - nonOverlapping[i] - 1; // gets the distance between them
            dp[i] = overlapping + dp[nonOverlapping[i]];
        }
    }

    int ans = dp[0] + numBeacons - 1;
    for (int i = 1; i < numBeacons; i++) {
        ans = min(dp[i] + (numBeacons - i - 1), ans);
    }
    cout << ans;
}

int main(int argc, char** argv) {
    int numBeacons;
    cin >> numBeacons;

    vector<pair<int, int> > beacons(numBeacons);

    for (int i = 0; i < numBeacons; i++) {
        int position, power;
        cin >> position >> power;
        beacons[i] = make_pair(position, power);
    }
    solve(beacons, numBeacons);
}


#include <iostream>
using namespace std;
const int maxn = 1e6 + 5;

int n, b[maxn], dp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n;
    for (int i = 0, a; i < n; i++) {
        cin >> a;
        cin >> b[a];
    }
    if (b[0] > 0) {
        dp[0] = 1;
    }
    int mx = 0;
    for (int i = 1; i < maxn; i++) {
        if (b[i] == 0) {
            dp[i] = dp[i - 1];
        } else {
            if (b[i] >= i) {
                dp[i] = 1;
            } else {
                dp[i] = dp[i - b[i] - 1] + 1;
            }
        }
        if (dp[i] > mx) {
            mx = dp[i];
        }
    }
    cout << n - mx << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int>     pii;
typedef long long   ll;
typedef vector<ll>  vi;
typedef long double ld;
#define PI     3.14159265
#define int long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define mtup make_tuple
#define nl '\n'
#define mem(cont,data)    memset(cont, data, sizeof(cont))
#define all(a) a.begin(), a.end()
#define REP(i,s,e) for(int i = s; i <= e; i++)
#define REPR(i,s,e) for(int i = s; i >= e; i--)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define len(thing) (int)thing.size()
#define FLASH ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define maxheap  priority_queue<ll>
#define minheap  priority_queue<ll,vector<ll> ,greater<ll>>
#define d0(a) cout<<a<<' ';
#define d1(a) cout<<a<<nl;
#define d2(a,b) cout<<a<<' '<<b<<nl;
#define d3(a,b,c) cout<<a<<' '<<b<<' '<<c<<nl;
#define d4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<nl;
#define d5(a,b,c,d,e) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<nl;
#define d6(a,b,c,d,e,f) cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<' '<<f<<nl;
const ll inf = 1e18 + 1;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
int maxx(int a,int b){return max(a,b);}
int minn(int a,int b){return min(a,b);}
ll mpow(ll a,ll b,int mod = LLONG_MAX) {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;}
template <typename T> bool cont(set<T> Set, T data){return Set.find(data) != Set.end();}
template <typename T> bool cont(multiset<T> Set, T data){return Set.find(data) != Set.end();}
int gcd(int a,int b) {return __gcd(a,b);}

void solve(){
  FLASH
  int n;cin>>n;
  vi a(1000000+5,0),b(1000000+5,0),pre(1000000+5,0),dp(1000000+5,0);
  int aa,bb;
  REP(i,1,n){
    cin>>aa>>bb;
    a[aa]=1;
    b[aa]=bb;
    pre[aa]=1;
  }
  int MAX=1000000+2;
  REP(i,1,MAX)pre[i]+=pre[i-1];
  int ans = inf;
  REP(i,0,MAX){
    if(!a[i] || !i){
      if(!i)continue;
      dp[i]=dp[i-1];
      continue;
    }
    int rigrem = pre[MAX]-pre[i];
    int lo = i - b[i], hi = i;
    int lefrem=0,pans=0;
    if (lo-1>=0) {
      pans=dp[lo-1];
      lefrem=pre[hi]-pre[lo-1]-1;
    }
    else lefrem=pre[i]-1;
    dp[i]=lefrem+pans;
  }
  ans=inf;
  REP(i,0,MAX){
    if(a[i]){
      ans=minn(ans,dp[i]+pre[MAX]-pre[i]);
    }
  }
  cout<<ans;
}

signed main(){
  FLASH
  int testcases = 1;
  //cin>>testcases;
  while(testcases--) solve();
  return 0;
}


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

#define lli long long int
#define f(j,m,n) for(lli j=m;j<n;j++)
using namespace std;

int main()
  {ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n;
   cin>>n;
   int a,b[1000001], dp[1000001];;

   int m=0;
   memset(b,0,sizeof(b));
   for(int i=1;i<=n;i++)
      {cin>>a;

       cin>>b[a];
       //color[a[i]]=1;
      }

    if(b[0]==0)
    dp[0]=0;
    else
     dp[0]=1;
     m=dp[0];
    for(lli i=1;i<1000001;i++)
      {if(b[i]==0)
         dp[i]=dp[i-1];
       else if(i-b[i]>0)
          dp[i]=dp[i-b[i]-1]+1;
       else
           dp[i]=1;
       if(dp[i]>m)
          m=dp[i];

      }
      cout<<n-m;

   }


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

#define lli long long int
#define f(j,m,n) for(lli j=m;j<n;j++)
using namespace std;

int main()
  {ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n;
   cin>>n;
   int a,b[1000001], dp[1000001];;

   int m=0;
   memset(b,0,sizeof(b));
   for(int i=1;i<=n;i++)
      {cin>>a;

       cin>>b[a];
       //color[a[i]]=1;
      }

    if(b[0]==0)
    dp[0]=0;
    else
     dp[0]=1;
     m=dp[0];
    for(lli i=1;i<=1000001;i++)
      {if(b[i]==0)
         dp[i]=dp[i-1];
       else if(i-b[i]-1>0)
          dp[i]=dp[i-b[i]-1]+1;
       else
           dp[i]=1;
       if(dp[i]>m)
          m=dp[i];

      }
      cout<<n-m;

   }


#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <chrono>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define vpii vector<pii>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vvd vector<vd>
#define pdd pair<double,double>
#define vpdd vector<pdd>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define search(set0, x) (set0.find(x)!=set0.end())
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define vvc vector<vc>
#define vvb vector<vb>
#define mp make_pair
#define f first
#define s second
#define pss pair<string, string>
#define vpss vector<pss>
#define double long double
#define vvs vector<vs>
#define mysort(vector) sort(vector.begin(), vector.end())
#define myreverse(vector) reverse(vector.begin(), vector.end())
#define printvi(vector) for (int x: vector) cout << x << " "; cout << endl;
#define printvll(vector) for (ll x: vector) cout << x << " "; cout << endl;
#define vvpii vector<vpii>
#define vvpll vector<vpll>

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod0 = 1e9 + 7;
const ll mod1 = 998244353;
const ll mod2 = 1e9 + 9;
const ll mod3 = 2147483647;

const int maxn = 1e5 + 3;
const ll inf = 2 * 1024 * 1024 * 1023;
ll n, m, k, t;

void init() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(16);
    //freopen("details.in", "r", stdin);
    //freopen("details.out", "w", stdout);

}

int main() {
    init();
    cin >> n;
    vpii a(maxn);
    for(int i = 0; i < n; ++i) cin >> a[i].f >> a[i].s;
    int ans = 0;
    int idx = 0;
    vi dp(1000005);
    for(int i = 0; i <= 1000000; i++) {
        if(i == a[idx].f) {
            int j = i - a[idx].s - 1;
            if(j >= 0) {
                dp[i] = dp[j] + 1;
            }
            else {
                dp[i] = 1;
            }
            idx++;
        }
        else {
            dp[i] = dp[i - 1];
        }
        ans = max(ans, dp[i]);
    }
    cout << n - ans << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <chrono>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define vpii vector<pii>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vvd vector<vd>
#define pdd pair<double,double>
#define vpdd vector<pdd>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define search(set0, x) (set0.find(x)!=set0.end())
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define vvc vector<vc>
#define vvb vector<vb>
#define mp make_pair
#define f first
#define s second
#define pss pair<string, string>
#define vpss vector<pss>
#define double long double
#define vvs vector<vs>
#define mysort(vector) sort(vector.begin(), vector.end())
#define myreverse(vector) reverse(vector.begin(), vector.end())
#define printvi(vector) for (int x: vector) cout << x << " "; cout << endl;
#define printvll(vector) for (ll x: vector) cout << x << " "; cout << endl;
#define vvpii vector<vpii>
#define vvpll vector<vpll>

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod0 = 1e9 + 7;
const ll mod1 = 998244353;
const ll mod2 = 1e9 + 9;
const ll mod3 = 2147483647;

const int maxn = 1e5 + 3;
const ll inf = 2 * 1024 * 1024 * 1023;
ll n, m, k, t;

void init() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(16);
    //freopen("details.in", "r", stdin);
    //freopen("details.out", "w", stdout);

}

int main() {
    init();
    cin >> n;
    vpii a(maxn, {inf, inf});
    for(int i = 0; i < n; ++i) cin >> a[i].f >> a[i].s;
    mysort(a);
    int ans = 0;
    int idx = 0;
    vi dp(1000005);
    for(int i = 0; i <= 1000000; i++) {
        if(i == a[idx].f) {
            int j = i - a[idx].s - 1;
            if(j >= 0) {
                dp[i] = dp[j] + 1;
            }
            else {
                dp[i] = 1;
            }
            idx++;
        }
        else {
            if (i > 0) dp[i] = dp[i - 1];
        }
        ans = max(ans, dp[i]);
    }
    cout << n - ans << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <chrono>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define vpii vector<pii>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vvd vector<vd>
#define pdd pair<double,double>
#define vpdd vector<pdd>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define search(set0, x) (set0.find(x)!=set0.end())
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define vvc vector<vc>
#define vvb vector<vb>
#define mp make_pair
#define f first
#define s second
#define pss pair<string, string>
#define vpss vector<pss>
#define double long double
#define vvs vector<vs>
#define mysort(vector) sort(vector.begin(), vector.end())
#define myreverse(vector) reverse(vector.begin(), vector.end())
#define printvi(vector) for (int x: vector) cout << x << " "; cout << endl;
#define printvll(vector) for (ll x: vector) cout << x << " "; cout << endl;
#define vvpii vector<vpii>
#define vvpll vector<vpll>

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod0 = 1e9 + 7;
const ll mod1 = 998244353;
const ll mod2 = 1e9 + 9;
const ll mod3 = 2147483647;

const int maxn = 1e5 + 3;
const ll inf = 2 * 1024 * 1024 * 1023;
ll n, m, k, t;

void init() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(16);
    //freopen("details.in", "r", stdin);
    //freopen("details.out", "w", stdout);

}

int main() {
    init();
    cin >> n;
    vpii a(maxn);
    for(int i = 0; i < n; ++i) cin >> a[i].f >> a[i].s;
    int ans = 0;
    int idx = 0;
    vi dp(1000005);
    for(int i = 0; i <= 1000000; i++) {
        if(i == a[idx].f) {
            int j = i - a[idx].s - 1;
            if(j >= 0) {
                dp[i] = dp[j] + 1;
            }
            else {
                dp[i] = 1;
            }
            idx++;
        }
        else {
            if (i > 0) dp[i] = dp[i - 1];
        }
        ans = max(ans, dp[i]);
    }
    cout << n - ans << endl;
    return 0;
}


//author : Brijesh1in
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define endl '\n'
#define boost ios::sync_with_stdio(false) ; cin.tie(NULL); cout.tie(NULL);
typedef long long li;

const int N = 1e6 + 10;
int dp[N];

void solve(){

	int n;
	cin>>n;
	pair<int,int> p[n];
	for(int i = 0 ; i < n ; ++i){
		cin>>p[i].ff>>p[i].ss;
		dp[p[i].ff] = p[i].ss;
	}

	sort(p,p+n);

	if(dp[0])
		dp[0] = 1;
	for(int i = 1 ; i < N ; ++i){

		if(dp[i]){

			int val = i - dp[i];

			if(val < 1)
				dp[i] = 1;
			else dp[i] = dp[val-1] + 1;
		}
		else dp[i] = dp[i-1];
	}

	int ans = 0;
	for(int i = 0 ; i < N ; ++i)
		ans = max(ans,dp[i]);

	cout<<n-ans<<endl;
	//cout<<dp[0]<<endl;
}

signed main(){

	boost;

	int t;
	t = 1;
	//cin>>t;

	while(t--)
		solve();

}


//author : Brijesh1in
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define endl '\n'
#define boost ios::sync_with_stdio(false) ; cin.tie(NULL); cout.tie(NULL);
typedef long long li;

const int N = 1e6 + 10;
int dp[N];

void solve(){

	int n;
	cin>>n;
	pair<int,int> p[n];
	for(int i = 0 ; i < n ; ++i){
		cin>>p[i].ff>>p[i].ss;
		dp[p[i].ff] = p[i].ss;
	}

	sort(p,p+n);

	dp[0] ? 1 : 0;
	for(int i = 1 ; i < N ; ++i){

		if(dp[i]){

			int val = i - dp[i];

			if(val < 1)
				dp[i] = 1;
			else dp[i] = dp[val-1] + 1;
		}
		else dp[i] = dp[i-1];
	}

	int ans = 0;
	for(int i = 0 ; i < N ; ++i)
		ans = max(ans,dp[i]);

	cout<<n-ans<<endl;
}

signed main(){

	boost;

	int t;
	t = 1;
	//cin>>t;

	while(t--)
		solve();

}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=1e9+7;
int a[N],b[N],p[10*N],vis[10*N],dp[10*N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vis[a[i]]=1,p[a[i]]=b[i];
	}
	int ans=0;
	for(int i=1;i<=10*N;i++){
		if(!vis[i]) dp[i]=dp[i-1];
		else{
			if(p[i]>=i) dp[i]=1;
			else dp[i]=dp[i-p[i]-1]+1;
		}
		ans=max(ans,dp[i]);
	}
	cout<<n-ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=1e9+7;
int a[N],b[N],p[10*N],vis[10*N],dp[10*N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vis[a[i]]=1,p[a[i]]=b[i];
	}
	int ans=0;
	for(int i=0;i<10*N;i++){
		if(!vis[i]) dp[i]=dp[i-1];
		else{
			if(p[i]>=i) dp[i]=1;
			else dp[i]=dp[i-p[i]-1]+1;
		}
		ans=max(ans,dp[i]);
	}
	cout<<n-ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
const LL mod=1e9+7;
int a[N],b[N],p[10*N],vis[10*N],dp[10*N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		vis[a[i]]=1,p[a[i]]=b[i];
	}
	int ans=0;
	for(int i=1;i<10*N;i++){
		if(!vis[i]) dp[i]=dp[i-1];
		else{
			if(p[i]>=i) dp[i]=1;
			else dp[i]=dp[i-p[i]-1]+1;
		}
		ans=max(ans,dp[i]);
	}
	cout<<n-ans<<endl;
    return 0;
}


/*
  Idea:
    - Dynamic Programming.
    - dp[i] represent the number of alive beacons until ith one and the new
      beacon in the right will kill all beacons after the ith one.
*/

#include <bits/stdc++.h>

using namespace std;

int const N = 1e6 + 1;
int n, dp[N], b[N];

int main() {
  scanf("%d", &n);
  for(int i = 0, a; i < n; ++i) {
    scanf("%d", &a);
    scanf("%d", b + a);
  }

  int res = 0;
  dp[0] = b[0] != 0;
  for(int i = 1; i < N; ++i) {
    if(b[i] == 0)
      dp[i] = dp[i - 1];
    else {
      if(b[i] >= i)
        dp[i] = 1;
      else
        dp[i] = dp[i - b[i] - 1] + 1;
    }

    res = max(res, dp[i]);
  }

  printf("%d\n", n - res);

  return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define vc vector<char>
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define si set<int>
#define pii pair<ll,ll>
#define pbi pair<bool,ll>
#define vpii vector<pii>
#define SORT(c) sort(c.begin(),c.end())
#define SORTC(c, cmp) sort(c.begin(),c.end(),cmp)
#define mapii map<int,int,greater<int>>
#define mapll map<ll,ll>
#define pb push_back
#define F first
#define S second

using namespace std;

int main() {
    int n{};
    cin >> n;
    int result{};
    mapii inputs{};
    int mx{};

    for (int i = 0; i < n; ++i) {
        int pos{}, pow{};
        cin >> pos >> pow;
        inputs[pos] = pow;
        mx = max(pos, mx);
    }

    vi dp(mx+1, 0);

    if (inputs[0])
        dp[0]=1;

    for (int j = 1; j <= mx; ++j) {
        if (!inputs[j]) {
            dp[j] = dp[j - 1];
        } else {
            int p = inputs[j];

            if (j - p - 1<0)
                dp[j]=1;
            else
                dp[j] = dp[j - p - 1] + 1;
        }
    }

//    for (int i:dp)
//        cout << i << " ";
//    cout << endl;

    result = *max_element(dp.begin(), dp.end());
    cout << n - result;

}


#include <bits/stdc++.h>

#define ll long long
#define vc vector<char>
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define si set<int>
#define pii pair<ll,ll>
#define pbi pair<bool,ll>
#define vpii vector<pii>
#define SORT(c) sort(c.begin(),c.end())
#define SORTC(c, cmp) sort(c.begin(),c.end(),cmp)
#define mapii map<int,int,greater<int>>
#define mapll map<ll,ll>
#define pb push_back
#define F first
#define S second

using namespace std;

int main() {
    int n{};
    cin >> n;
    int result{};
    mapii inputs{};
    int mx{};

    for (int i = 0; i < n; ++i) {
        int pos{}, pow{};
        cin >> pos >> pow;
        inputs[pos] = pow;
        mx = max(pos, mx);
    }

    vi dp(mx+1, 0);

    for (int j = 1; j <= mx; ++j) {
        if (!inputs[j]) {
            dp[j] = dp[j - 1];
        } else {
            int p = inputs[j];
            dp[j] = dp[max(j - p - 1, 0)] + 1;
        }
    }

//    for (int i:dp)
//        cout << i << " ";
//    cout << endl;

    result = *max_element(dp.begin(), dp.end());
    cout << n - result;

}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 10;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	int n;
	cin>>n;
	vector<int> lft(maxn);
	int a, b;
	for(int i = 0; i < n; i++) {
		cin>>a>>b;
		lft[a] = b;
	}
	int ans = 0;
	for(int i = 0; i < 8; i++) {
		if(lft[i] == 0)
			if(i > 0) lft[i] = lft[i - 1];
			else lft[i] = 0;
		else {
			int p = i - 1 - lft[i];
			if(p > 0) lft[i] = 1 + lft[p];
			else lft[i] = 1;
		}
		ans = max(ans, lft[i]);
	}
	ans = n - ans;
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 10;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	int n;
	cin>>n;
	vector<int> lft(maxn);
	int a, b;
	for(int i = 0; i < n; i++) {
		cin>>a>>b;
		lft[a] = b;
	}
	int ans = 0;
	for(int i = 0; i < maxn; i++) {
		if(lft[i] == 0)
			if(i > 0) lft[i] = lft[i - 1];
			else lft[i] = 0;
		else {
			int p = i - 1 - lft[i];
			if(p > 0) lft[i] = 1 + lft[p];
			else lft[i] = 1;
		}
		ans = max(ans, lft[i]);
	}
	ans = n - ans;
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 10;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	int n;
	cin>>n;
	vector<int> lft(maxn);
	int a, b;
	for(int i = 0; i < n; i++) {
		cin>>a>>b;
		lft[a] = b;
	}
	int ans = 0;
	for(int i = 0; i < maxn; i++) {
		if(lft[i] == 0)
			if(i > 0) lft[i] = lft[i - 1];
			else lft[i] = 0;
		else {
			int p = i - 1 - lft[i];
			if(p >= 0) lft[i] = 1 + lft[p];
			else lft[i] = 1;
		}
		ans = max(ans, lft[i]);
	}
	ans = n - ans;
	cout<<ans;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
vector< pair<int,int> > bombs;
int poss[100005];
int values[100005];

int last( int p){
    int lpos = bombs[p].first - bombs[p].second-1;
    if( binary_search(values,values+n , lpos ) ){
        return lower_bound(values,values+n, lpos) - values;
    }
    else return lower_bound(values,values+n, lpos) - values -1 ;

}

int dp(int p ){
    if( p < 0) return 0;
    if(poss[p] != -1) return poss[p];

    return poss[p] = dp( last(p)  ) +1 ;

}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;
   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs.push_back({t1,t2});
    values[i] = t1;
   }
   memset(poss,-1,sizeof poss);
   int mx = 0;
   int x = 0;
   lp(i,0,n){
        if(dp(i) >= mx ) mx = dp(i);
   }
   cout << n - mx ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
int bombs [100005][2];
int poss[100005];

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;

   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs[t1][0] = 1;
    bombs[t1][1] = t2;
   }
    int mx = 0;
   lp(i,0,t1+1){
      if( bombs[i][0]  ){
        int l = i - bombs[i][1] -1 ;
        //cout <<i << " " <<l << endl;
        if(l< 0) l = 1;
        else l = poss[l]+1;
        poss[i] = l;

      }
      else if(i != 0 ) poss[i] = poss[i-1];

      mx = max(mx,poss[i]);
   }

cout << n - mx;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
int bombs [100005][2];
int poss[100005];
int values[100005];

int last( int p){
    int lpos = bombs[p][0] - bombs[p][1]-1;
    int l = 0 , r = n-1;
    while(l<=r){
        int md = (l+r)/2;
        if(values[md]==lpos) return md;
        if(values[md] > lpos) r = md -1;
        else l = md +1;
    }
    return r;

}

int dp(int p ){
    if( p < 0) return 0;
    if(poss[p] != -1) return poss[p];

    return poss[p] = dp( last(p)  ) +1 ;
}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;

   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs[i][0] = t1;
    bombs[i][1] = t2;
    values[i] = t1;
   }
   memset(poss,-1,sizeof poss);
   int mx = 0;
   int x = 0;
   lp(i,0,n){
        if(dp(i) >= mx ) mx = dp(i);
   }
   cout << n - mx ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
vector< pair<int,int> > bombs;
int poss[100005];
vi values;

int last( int p){
    int lpos = bombs[p].first - bombs[p].second-1;
    if( binary_search(values.begin(),values.end() , lpos ) ){
        return lower_bound(values.begin(),values.end() , lpos) - values.begin();
    }
    else return lower_bound(values.begin(),values.end() , lpos) - values.begin() -1 ;

}

int dp(int p ){
    if( p < 0) return 0;
    if(poss[p] != -1) return poss[p];

    return poss[p] = dp( last(p)  ) +1 ;

}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;
   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs.push_back({t1,t2});
    values.push_back(t1);
   }
   sort(bombs.begin(),bombs.end());
   sort(values.begin(),values.end());
   memset(poss,-1,sizeof poss);
   int mx = 0;
   int x = 0;
   lp(i,0,n){
        if(dp(i) >= mx ) mx = dp(i);
   }
   cout << n - mx ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
int bombs [100005][2];
int poss[100005];
int values[100005];

int last( int p){
    int lpos = bombs[p][0] - bombs[p][1]-1;
    if( binary_search(values,values+n , lpos ) ){
        return lower_bound(values,values+n, lpos) - values;
    }
    else return lower_bound(values,values+n, lpos) - values -1 ;

}

int dp(int p ){
    if( p < 0) return 0;
    if(poss[p] != -1) return poss[p];

    return poss[p] = dp( last(p)  ) +1 ;
}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;

   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs[i][0] = t1;
    bombs[i][1] = t2;
    values[i] = t1;
   }
   memset(poss,-1,sizeof poss);
   int mx = 0;
   int x = 0;
   lp(i,0,n){
        if(dp(i) >= mx ) mx = dp(i);
   }
   cout << n - mx ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
vector< pair<int,int> > bombs;
int poss[100005];
vi values;

int last( int p){
    int lpos = bombs[p].first - bombs[p].second-1;
    if( binary_search(values.begin(),values.end() , lpos ) ){
        return lower_bound(values.begin(),values.end() , lpos) - values.begin();
    }
    else return lower_bound(values.begin(),values.end() , lpos) - values.begin() -1 ;

}

int dp(int p ){
    if( p < 0) return 0;
    if(poss[p] != -1) return poss[p];

    return poss[p] = dp( last(p)  ) +1 ;

}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;
   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs.push_back({t1,t2});
    values.push_back(t1);
   }
   memset(poss,-1,sizeof poss);
   int mx = 0;
   int x = 0;
   lp(i,0,n){
        if(dp(i) >= mx ) mx = dp(i);
   }
   cout << n - mx ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long) 1e15;
#define lp(i,a,n) for(long long i = a ; i < n ; ++i)
#define rep(i,vec) for(ll i = 0 ; i < vec.size() ; ++i)
#define reprv(i,vec) for(ll i = vec.size()-1; i >=0 ; --i)
#define eps (1e-5)
#define pb(i) push_back(i)
#define PI acos(-1.0)
#define vec(a,b)                ((b)-(a))
#define length(a)               (hypot((a).imag(), (a).real()))
typedef vector<int> vi ;
typedef   long long ll;
typedef vector<ll> vll ;
typedef pair<int,int> pii ;
typedef vector< pair<int,int> >  vpii;
typedef vector<vector<int> > vvi ;
typedef complex<double> point;

using namespace std;
ll  Pow(ll  B,ll  P){ll  R=1; while(P>0)  {if(P&1) R=(R*B);P/=2;B=(B*B);}return R;} //compute b^p
ll  GCD(ll  a,ll  b){ while(b){b ^= a ^=b ^= a %=b;}  return a;}
ll  LCM(ll  a,ll  b) { return (a / GCD(a,b)*b); }
ll  BigMod(ll  B,ll  P,ll  M){ ll  R=1; while(P>0)  {if(P&1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (ll )R;} //compute b^p%m

int n ;
int bombs [100005][2];
int poss[100005];
int values[100005];

int last( int p){
    int lpos = bombs[p][0] - bombs[p][1]-1;
    int l = 0 , r = n-1;
    while(l<=r){
        int md = (l+r)/2;
        if(values[md]==lpos) return md;
        if(values[md] > lpos) r = md -1;
        else l = md +1;
    }
    return r;

}

int main()
{
    ios::sync_with_stdio(0);
  //  cin.tie(0);
//    freopen( "output.txt" , "w" , stdout ) ;
//    freopen( "input.txt" , "r" , stdin ) ;
    cin >> n ;

   int t1,t2;
   lp(i,0,n){
    cin >> t1>>t2;
    bombs[i][0] = t1;
    bombs[i][1] = t2;
    values[i] = t1;
   }

   int mx = 0;
   lp(i,0,n){
        int before = last(i);
        //cout << before<<" ";
        if(before < 0 ) before = 0;
        else before = poss[before];
        poss[i] = before + 1 ;

        mx = max(mx,poss[i]);

   }
   cout << n - mx ;

    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#define int long long int
using namespace std;
int inf=1e18;
void solve()
{
	int n; cin>>n;
	vector<pair<int, int>> v;
	set<int> s;
	for (int j=0; j<n; j++)
	{
		int a, b; cin>>a>>b;
		v.push_back({a, b});
		s.insert(a);
	}
	sort(v.begin(), v.end());
	vector<int> dp(n);
	for (int j=0; j<n; j++)
	{
		pair<int, int> p1={v[j].first-v[j].second, 0};
		auto it=lower_bound(v.begin(), v.end(), p1);
		if (it==v.begin())
		{
			dp[j]=1;
		}
		else
		{
			it=prev(it);
			int i=it-v.begin();
			dp[j]=1+dp[i];
		}
	}
	int ans=0;
	for (int j=n-1; j>=0; j--)
	{
		ans=max(ans, dp[j]);
	}
	ans++;
	cout<<n+1-ans<<"\n";
	return;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	//cin >> t;
	t=1;
	while (t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define gcd __gcd
#define Fi first
#define Se second
//#define mp make_pair
#define lowbit(x) ((x)&-(x))
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e6+7;
const int M = 1e6+7;
const int lim = 2e6;
const int P = 1e9+7;
const int inf = 0x3f3f3f3f;
int b[N],dp[N];
int main()
{
	int n,mx=0,ans=0;
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%d",&b[x]);
		mx = max(x,mx);
	}
	for(int i=1;i<=mx;i++)
	{
//		printf("%d__%d\n",i,b[i]);
		if( b[i] == 0 )
			dp[i] = dp[i-1];
		else if( b[i] > i )
			dp[i] = 1;
		else
			dp[i] = dp[i-b[i]-1]+1;
		ans = max(dp[i],ans);
	}
	printf("%d\n",n-ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define gcd __gcd
#define Fi first
#define Se second
//#define mp make_pair
#define lowbit(x) ((x)&-(x))
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e6+7;
const int M = 1e6+7;
const int lim = 2e6;
const int P = 1e9+7;
const int inf = 0x3f3f3f3f;
int b[N],dp[N];
int main()
{
	int n,mx=0,ans=0;
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%d",&b[x]);
		mx = max(x,mx);
	}
	for(int i=0;i<=mx;i++)
	{
//		printf("%d__%d\n",i,b[i]);
		if( b[i] == 0 )
		{
			if( i > 0 )
				dp[i] = dp[i-1];
		}
		else if( b[i] >= i )
			dp[i] = 1;
		else
			dp[i] = dp[i-b[i]-1]+1;
		ans = max(dp[i],ans);
	}
	printf("%d\n",n-ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define gcd __gcd
#define Fi first
#define Se second
//#define mp make_pair
#define lowbit(x) ((x)&-(x))
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e6+7;
const int M = 1e6+7;
const int lim = 2e6;
const int P = 1e9+7;
const int inf = 0x3f3f3f3f;
int b[N],dp[N];
int main()
{
	int n,mx=0,ans=0;
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%d",&b[x]);
		mx = max(x,mx);
	}
	for(int i=0;i<=mx;i++)
	{
//		printf("%d__%d\n",i,b[i]);
		if( b[i] == 0 && i != 0 )
			dp[i] = dp[i-1];
		else if( b[i] >= i )
			dp[i] = 1;
		else
			dp[i] = dp[i-b[i]-1]+1;
		ans = max(dp[i],ans);
	}
	printf("%d\n",n-ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define gcd __gcd
#define Fi first
#define Se second
//#define mp make_pair
#define lowbit(x) ((x)&-(x))
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e6+7;
const int M = 1e6+7;
const int lim = 2e6;
const int P = 1e9+7;
const int inf = 0x3f3f3f3f;
int b[N],dp[N];
int main()
{
	int n,mx=0,ans=0;
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++)
	{
		scanf("%d",&x);
		scanf("%d",&b[x]);
		mx = max(x,mx);
	}
	for(int i=1;i<=mx;i++)
	{
//		printf("%d__%d\n",i,b[i]);
		if( b[i] == 0 )
			dp[i] = dp[i-1];
		else if( b[i] >= i )
			dp[i] = 1;
		else
			dp[i] = dp[i-b[i]-1]+1;
		ans = max(dp[i],ans);
	}
	printf("%d\n",n-ans);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gaurav Tiwari
 */

#include <bits/stdc++.h>
#define mod 1000000007ll
#define mod2 100000009ll
#define mod3 998244353
#define pb push_back
#define fastIO ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define readi(x) scanf("%d",&x)
#define reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define PI 3.141592653589793238462643383
#define repi(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>b;i--)
#define mp make_pair
#define ll long long
#define sorti(a,b) sort(a,b)
#define sortd(a,b,tp) sort(a,b,greater<tp>())
#define fi first
#define se second

using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<long double,long double>pdd;
template<class T>
using max_pq = priority_queue<T>;
template<class T>
using min_pq = priority_queue<T,vector<T>,greater<T>>;

int cache[100008][2],n,position[100008],power[100008];

int dp(int i,int taken)
{
    if(i==0)
        return 0;
    if(cache[i][taken]!=-1)
        return cache[i][taken];

    if(taken==0) {
        cache[i][taken]= min(dp(i - 1, 0), dp(i - 1, 1)) + 1;
        return cache[i][taken];
    }
    int x=i-1,destroyed=0;

    while(x>-1 && position[i]-position[x]<=power[i])
    {
        x--;
        destroyed++;
    }

    if(x>-1)
    {
        cache[i][taken]= dp(x,1)+destroyed;
        return cache[i][taken];
    }

}

class CChainReaction {
public:
	void solve(istream& cin, ostream& cout) {
        cin>>n;
        memset(cache,-1, sizeof(cache));
        repi(i,0,n)
        {
            cin>>position[i]>>power[i];
        }
        cout<<min(dp(n-1,0),dp(n-1,1));
	}
};

int main() {
    fastIO;
	CChainReaction solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gaurav Tiwari
 */

#include <bits/stdc++.h>
#define mod 1000000007ll
#define mod2 100000009ll
#define mod3 998244353
#define pb push_back
#define fastIO ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define readi(x) scanf("%d",&x)
#define reads(x)  scanf("%s", x)
#define readl(x) scanf("%I64d",&x)
#define PI 3.141592653589793238462643383
#define repi(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>b;i--)
#define mp make_pair
#define ll long long
#define sorti(a,b) sort(a,b)
#define sortd(a,b,tp) sort(a,b,greater<tp>())
#define fi first
#define se second

using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<long double,long double>pdd;
template<class T>
using max_pq = priority_queue<T>;
template<class T>
using min_pq = priority_queue<T,vector<T>,greater<T>>;

int cache[100008][2],n;
vector<pair<int,int>>arr;

int binarysearch(int i,int j,int x)
{
    int l=i,r=j,ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(arr[mid].fi>=x)
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    if(ans==-1)
    {
            return j+1;
    }
    return ans;
}

int dp(int i,int taken)
{
    if(i<=0)
        return 0;
    if(cache[i][taken]!=-1)
        return cache[i][taken];

    if(taken==0) {
        cache[i][taken]= min(dp(i - 1, 0), dp(i - 1, 1)) + 1;
        return cache[i][taken];
    }

    int x=binarysearch(0,i-1,arr[i].fi-arr[i].se);
    if(x!=i)
    {
        cache[i][taken]=dp(x-1,1)+i-x;
        return cache[i][taken];
    }
    else{
        cache[i][taken]=dp(i-1,1);
        return cache[i][taken];
    }
}

class CChainReaction {
public:
	void solve(istream& cin, ostream& cout) {
	    arr.clear();
        cin>>n;
        memset(cache,-1, sizeof(cache));
        repi(i,0,n)
        {
            int a,b;
            cin>>a>>b;
            arr.pb({a,b});
        }
        sort(arr.begin(),arr.end());
        cout<<min(dp(n-1,0),dp(n-1,1));
	}
};

int main() {
    fastIO;
	CChainReaction solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


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

pair<int,int> A[100010];

int n,dp[100010][2];

int destroyed(int i, bool act){
	if(i < 0){
		return 0;
	}
	if(dp[i][act] != -1){
		return dp[i][act];
	}
	if(act){
		int j = lower_bound(A,A+n,make_pair(A[i].first-A[i].second,-1)) - A;
		int ret = (i-j) + destroyed(j-1, true);
		for(int k=j;k<i;k++){
			dp[k][false] = ret;
		}
		return dp[i][act] = ret;
	}else{
		int ret = 1 + destroyed(i-1, true);
		return dp[i][act] = ret;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	memset(dp,-1,sizeof(dp));
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> A[i].first >> A[i].second;
	}
	sort(A,A+n);

	int ans = n;
	for(int i=0;i<n;i++){
		ans = min(ans, i + destroyed(n-i-1, true));
	}
	cout << ans << '\n';

	return 0;
}


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

pair<int,int> A[100010];

int n,dp[100010][2];

int destroyed(int i, bool act){
	if(i < 0){
		return 0;
	}
	if(dp[i][act] != -1){
		return dp[i][act];
	}
	if(act){
		int j = lower_bound(A,A+n,make_pair(A[i].first-A[i].second,-1)) - A;
		int ret = (i-j) + destroyed(j-1, true);
		return dp[i][act] = ret;
	}else{
		int ret = 1 + destroyed(i-1, true);
		return dp[i][act] = ret;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	memset(dp,-1,sizeof(dp));
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> A[i].first >> A[i].second;
	}
	sort(A,A+n);

	int ans = n;
	for(int i=0;i<n;i++){
		ans = min(ans, i + destroyed(n-i-1, true));
	}
	cout << ans << '\n';

	return 0;
}


/*
    Stay motivated and keep working hard
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, bool> beacons;
map<ll, ll> strength;
ll prefix[1000007] = {0}, ctr[1000007] = {0};
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        // freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n;
    cin >> n;
    ll a[n], b[n];
    ll mxm = 0, mnm = INT_MAX;
    for(ll i=0;i<n;i++){
        cin >> a[i] >> b[i];
        beacons[a[i]] = true;
        strength[a[i]] = b[i];
        ctr[a[i]] = 1;
        mxm = max(mxm, a[i]);
        mnm = min(mnm, a[i]);
    }
    ll ans = n;
    ll good = n, expl = 0;
    for(ll i=1;i<=mxm;i++){
        ctr[i] += ctr[i-1];
    }
    ll maxStr = 0, maxIdx = -1;
    for(ll i=0;i<=1000000;i++){
        if(beacons[i]){
            // if(i - strength[i] - 1 >= 0 && i - 1 >= 0){
                // prefix[i] = ctr[i-1] - ctr[i - 1 - strength[i]];
                // prefix[i] = prefix[max(0LL, i-1-strength[i])] + 1;
            // }
            if(i - 1 - strength[i] < 0){
                prefix[i] = 1;
            }
            else{
                prefix[i] = prefix[i-1-strength[i]] + 1;
            }
            if(maxStr < prefix[i]){
                maxStr = prefix[i];
                maxIdx = i;
            }
        }
        else{
            prefix[i] = prefix[i-1];
        }
    }
    // for(ll i=mnm; i<=mxm; i++){
    //     cout << prefix[i] << " ";
    // }
    // cout << "\n";
    // cout << maxStr << " " << maxIdx << "\n";
    // ll ans1 = 0;
    cout << n - maxStr << "\n";

    #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif
    return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;

#define PB push_back
#define MP make_pair
#define AA first
#define BB second
#define SZ size()
#define BG begin()
#define OP begin()
#define ED end()
#define SQ(x) ((x)*(x))
#define cmax(x, y) x = max(x, y)
#define cmin(x, y) x = min(x, y)

const int MAXN = 100005;
PII b[MAXN];
int dp[MAXN];

int solve(int i, int n){
	if(dp[i] != -1)
		return dp[i];
	else if(i == 0)
		return dp[i] = 0;

	int dist = b[i].AA - b[i].BB;

	PII temp = {dist, 0};

	auto low = lower_bound(b, b + n, temp);
	int index = low - b;

	while(b[index].AA >= dist){
		index--;
	}

	//cout << i << " " << index << endl;

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

	return dp[i] = i - index - 1 + solve(index, n);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int N;
	cin >> N;

	for(int i = 0; i <= N; i++)
		dp[i] = -1;

	b[0].AA = -2000000;
	for(int i = 1; i <= N; i++){
		cin >> b[i].AA;
		cin >> b[i].BB;
	}

	sort(b, b + N + 1);

	int minimum = N + 1;
	for(int i = N; i >= 0; i--){
		cmin(minimum, (N - i) + solve(i, N + 1));
	}

	cout << minimum << endl;
    return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;

#define PB push_back
#define MP make_pair
#define AA first
#define BB second
#define SZ size()
#define BG begin()
#define OP begin()
#define ED end()
#define SQ(x) ((x)*(x))
#define cmax(x, y) x = max(x, y)
#define cmin(x, y) x = min(x, y)

const int MAXN = 100005;
PII b[MAXN];
int dp[MAXN];

int solve(int i, int n){
	if(dp[i] != -1)
		return dp[i];
	else if(i == 0)
		return dp[i] = 0;

	int dist = b[i].AA - b[i].BB;

	PII temp = {dist, 0};

	auto low = lower_bound(b, b + n, temp);
	int index = low - b;

	while(b[index].AA >= dist){
		index--;
	}

	//cout << i << " " << index << endl;

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

	return dp[i] = i - index - 1 + solve(index, n);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int N;
	cin >> N;

	for(int i = 0; i <= N; i++)
		dp[i] = -1;

	b[0].AA = -2000000;
	for(int i = 1; i <= N; i++){
		cin >> b[i].AA;
		cin >> b[i].BB;
	}

	int minimum = N + 1;
	for(int i = N; i >= 0; i--){
		cmin(minimum, (N - i) + solve(i, N + 1));
	}

	cout << minimum << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = 100005;

int n;
pair<int, int> a[MaxN];
int dp[MaxN];

int BS(int Left, int Right, int x)
{
    while (Left <= Right)
    {
        int Mid = (Left + Right) / 2;
        if (a[Left].first < x)
        {
            Left = Mid + 1;
        }
        else
        {
            Right = Mid - 1;
        }
    }
    return Right;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].first >> a[i].second;
    }
    sort(a + 1, a + n + 1);
    dp[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        int j = BS(1, i - 1, a[i].first - a[i].second);
        dp[i] = dp[j] + 1;
    }
    cout << n - *max_element(dp + 1, dp + n + 1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = 1000005;

int n;
int Power[MaxN], dp[MaxN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    int a, b;
    int Max = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        Power[a] = b;
        Max = max(Max, a);
    }
    dp[0] = (Power[0] != 0);
    for (int i = 1; i <= Max; i++)
    {
        if (Power[i] == 0)
        {
            dp[i] = dp[i - 1];
        }
        else
        {
            dp[i] = (Power[i] >= i ? 1 : dp[i - Power[i] - 1] + 1);
        }
    }
    cout << n - *max_element(dp, dp + Max + 1);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef double dd;
#define pb push_back
#define mp make_pair
#define endl "\n"
ll const M = 1000000007;
dd const pi = acos(-1);
ll const inf = 9e18;
ll const N = 1000000;
int main()
{
    fast;
  	ll i,j,t,n;
  	cin >> n;
  	ll a[n + 1],b[n + 1],dp[n + 1];
  	vector<pair<ll,ll> > temp;
  	temp.pb({-1,-1});
  	a[0] = b[0] = -inf;
  	dp[0] = dp[1] = 0;
  	for(i = 1; i <= n; i++)
  	{
  		ll l,r;
  		cin >> l >> r;
  		temp.pb({l,r});
  	}
  	sort(temp.begin(),temp.end());
  	for(i = 1; i <= n; i++)
  	{
  		a[i] = temp[i].first;
  		b[i] = temp[i].second;
  	}
  	for(i = 2; i <= n; i++)
  	{
  		ll index = lower_bound(a,a + n + 1,a[i] - b[i]) - a;
  		dp[i] = i - index + dp[index - 1];
  	}
  	ll ans = dp[n],x = a[n] + 1;
  	for(i = 1; i <= N; i++)
  	{
  		ll index = lower_bound(a,a + n + 1,x - i) - a;
  		ans = min(ans,(n + 1) - index + dp[index - 1]);
  	}
  	cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef double dd;
#define pb push_back
#define mp make_pair
#define endl "\n"
ll const M = 1000000007;
dd const pi = acos(-1);
ll const inf = 9e18;
ll const N = 1000000;
int main()
{
    fast;
  	ll i,j,t,n;
  	cin >> n;
  	ll a[n + 1],b[n + 1],dp[n + 1];
  	a[0] = b[0] = -inf;
  	dp[0] = dp[1] = 0;
  	for(i = 1; i <= n; i++)
  		cin >> a[i] >> b[i];
  	for(i = 2; i <= n; i++)
  	{
  		ll index = lower_bound(a,a + n + 1,a[i] - b[i]) - a;
  		dp[i] = i - index + dp[index - 1];
  	}
  	ll ans = dp[n],x = a[n] + 1;
  	for(i = 1; i <= N; i++)
  	{
  		ll index = lower_bound(a,a + n + 1,x - i) - a;
  		ans = min(ans,(n + 1) - index + dp[index - 1]);
  	}
  	cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(ll i=0;i<n;i++)
#define fa(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,n) for(ll i=n-1;i>=0;i--)
#define endl "\n"
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ms(array,value) memset(array,value,sizeof(array))
#define test(t) ll t;cin>>t;while(t--)
#define en cout<<endl;
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int> >
#define pb push_back
#define all(a) a.begin(),a.end()
#define pi pair<int,int>
#define mod2  998,244,35

int main()
{
    sync
    //clock_t launch=clock();
    int n;
    cin>>n;
    vi a(1000005,0);
    int mx=0;
    int x,y;
    f(i,n)
    {
        cin>>x>>y;
        a[x]=y;
        mx=x;
    }
    vi dp(mx+5,0);
    dp[0]= a[0]>0?1:0;
    int cnt=dp[0];
    fa(i,1,mx)
    {
        //cout<<i<<endl;
        if(a[i]==0) dp[i]=dp[i-1];
        else
        {
            int x=i-a[i]-1;
            if(x>=0)  dp[i]= dp[x]+1;
            else dp[i]=1;
        }
        cnt=max(cnt,dp[i]);
    }
    //cout<<n<<" "<<cnt<<endl;
    cout<<n-cnt;
    //clog<<((long double)(clock()-launch)/CLOCKS_PER_SEC)<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(ll i=0;i<n;i++)
#define fa(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,n) for(ll i=n-1;i>=0;i--)
#define endl "\n"
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ms(array,value) memset(array,value,sizeof(array))
#define test(t) ll t;cin>>t;while(t--)
#define en cout<<endl;
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int> >
#define pb push_back
#define all(a) a.begin(),a.end()
#define pi pair<int,int>
#define mod2  998,244,35

int main()
{
    int n;
    cin>>n;
    vi a(100005,0);
    int mx=0;
    int x,y;
    f(i,n)
    {
        cin>>x>>y;
        a[x]=y;
        mx=x;
    }
    int dp[mx+5]={0};
    dp[0]= a[0]?1:0;
    int cnt=0;
    fa(i,1,mx)
    {
        if(a[i]==0) dp[i]=dp[i-1];
        else
        {
            int x=i-a[i]-1;
            if(x>=0)  dp[i]= dp[x]+1;
            else dp[i]=1;
        }
        cnt=max(cnt,dp[i]);
    }
    //cout<<n<<" "<<cnt<<endl;
    cout<<n-cnt;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(ll i=0;i<n;i++)
#define fa(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,n) for(ll i=n-1;i>=0;i--)
#define endl "\n"
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ms(array,value) memset(array,value,sizeof(array))
#define test(t) ll t;cin>>t;while(t--)
#define en cout<<endl;
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int> >
#define pb push_back
#define all(a) a.begin(),a.end()
#define pi pair<int,int>
#define mod2  998,244,35

const int N=1e6+5;
int dp[N],a[N];
int main()
{
    sync
    //clock_t launch=clock();
    int n;
    cin>>n;
    int mx;
    int x,y;
    f(i,n)
    {
        cin>>x>>y;
        a[x]=y;
        mx=x;
    }
    dp[0]= a[0]>0?1:0;
    int cnt=0;
    fa(i,1,N)
    {
        //cout<<i<<endl;
        if(a[i]==0) dp[i]=dp[i-1];
        else
        {
            int x=i-a[i]-1;
            if(x>=0)  dp[i]= dp[x]+1;
            else dp[i]=1;
        }
        cnt=max(cnt,dp[i]);
    }
    //cout<<n<<" "<<cnt<<endl;
    cout<<n-cnt;
    //clog<<((long double)(clock()-launch)/CLOCKS_PER_SEC)<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(ll i=0;i<n;i++)
#define fa(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,n) for(ll i=n-1;i>=0;i--)
#define endl "\n"
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ms(array,value) memset(array,value,sizeof(array))
#define test(t) ll t;cin>>t;while(t--)
#define en cout<<endl;
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int> >
#define pb push_back
#define all(a) a.begin(),a.end()
#define pi pair<int,int>
#define mod2  998,244,35

int main()
{
    int n;
    cin>>n;
    vi a(100005,0);
    int mx=0;
    int x,y;
    f(i,n)
    {
        cin>>x>>y;
        a[x]=y;
        mx=x;
    }
    int dp[mx+5]={0};
    dp[0]= a[0]?1:0;
    int cnt=dp[0];
    fa(i,1,mx)
    {
        if(a[i]==0) dp[i]=dp[i-1];
        else
        {
            int x=i-a[i]-1;
            if(x>=0)  dp[i]= dp[x]+1;
            else dp[i]=1;
        }
        cnt=max(cnt,dp[i]);
    }
    //cout<<n<<" "<<cnt<<endl;
    cout<<n-cnt;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];

       fr(k,1,a*b)
        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                if(v[i+1][j]==v[i][j]||v[i-1][j]==v[i][j]||v[i][j+1]==v[i][j]||v[i][j-1]==v[i][j])
                {
                   if(v[i-1][j]==v[i][j]+1||v[i][j-1]==v[i][j]+1) continue;

                    v[i][j]++;
                    vis[i][j]=1;

                }
            }
        }

    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];
    fr(k,1,100)
    {

        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                if(v[i+1][j]==v[i][j]||v[i-1][j]==v[i][j]||v[i][j+1]==v[i][j]||v[i][j-1]==v[i][j])
                {
                    v[i][j]++;
                    vis[i][j]=1;

                }
            }
        }
    }
    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];

    for(ll k=4; k>=1; k--)
    {

    ll cnt=100;
    while(cnt--)
        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                ll p=0;
                if(v[i][j]==v[i+1][j])
                    p++;
                if(v[i][j]==v[i-1][j])
                    p++;
                if(v[i][j]==v[i][j+1])
                    p++;
                if(v[i][j]==v[i][j-1])
                    p++;

                if(p==k)
                {
                    v[i][j]++;
                    vis[i][j]=1;

                }
            }
        }
    }

    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];

        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                if(v[i+1][j]==v[i][j]||v[i-1][j]==v[i][j]||v[i][j+1]==v[i][j]||v[i][j-1]==v[i][j])
                {

                    v[i][j]++;
                    vis[i][j]=1;

                }
            }
        }
        fr(i,1,b)
        {
            fr(j,1,a)
            {
                if(vis[i][j])
                    continue;
                if(v[j+1][i]==v[j][i]||v[j-1][i]==v[j][i]||v[j][i+1]==v[j][i]||v[j][i-1]==v[j][i])
                {

                    v[j][i]++;
                    vis[j][i]=1;

                }
            }
        }

    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];
    fr(k,1,100)
    {

        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                if(v[i+1][j]==v[i][j]||v[i-1][j]==v[i][j]||v[i][j+1]==v[i][j]||v[i][j-1]==v[i][j])
                {
                    v[i][j]++;
                    vis[i][j]=1;
                    vis[i+1][j]=1;
                    vis[i-1][j]=1;
                    vis[i][j+1]=1;
                    vis[i][j-1]=1;
                }
            }
        }
    }
    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}


/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ld long double
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
ll v[105][105];
bool vis[105][105];
void solve()
{
    memset(v,0,sizeof v);
    memset(vis,false,sizeof vis);
    ll a,b;
    cin>>a>>b;
    fr(i,1,a) fr(j,1,b) cin>>v[i][j];

    for(ll k=4; k>=1; k--)
    {

        fr(i,1,a)
        {
            fr(j,1,b)
            {
                if(vis[i][j])
                    continue;
                ll p=0;
                if(v[i][j]==v[i+1][j])
                    p++;
                if(v[i][j]==v[i-1][j])
                    p++;
                if(v[i][j]==v[i][j+1])
                    p++;
                if(v[i][j]==v[i][j-1])
                    p++;

                if(p==k)
                {
                    v[i][j]++;
                    vis[i][j]=1;

                }
            }
        }
    }

    fr(i,1,a)
    {
        fr(j,1,b) cout<<v[i][j]<<' ';
        cout<<endl;
    }
}
int main()
{
    rj
    tst
    solve();
    return 0;
}