#include<bits/stdc++.h>
//using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i,j,n) for(ll i=(ll)(j);i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(ll)(j);i<=(ll)(n);i++)
#define per(i,j,n) for(ll i=(ll)(j);(ll)(n)<=i;i--)
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(ll)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
//using std::endl;
using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::upper_bound;
using std::lower_bound;
using vi=vector<ll>;
using vii=vector<vi>;
using pii=std::pair<ll,ll>;
//
constexpr ll MOD=1e9+7;
//constexpr ll MOD=998244353;
//constexpr ll MOD=10000000;
//constexpr ll MOD=1e4;
//constexpr ll MOD=1e5;
constexpr ll MAX=3e6;
constexpr ll inf=(1ll<<60);
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template<typename T>
struct Segment_tree{
    ll N;
    T mem;
    vector<T> node;
    Segment_tree(vector<T> &X,T m):mem(m){
        ll sz=X.size();
        N=1;
        while(N<sz) N*=2;
        node.resize(2*N-1,mem);
        rep(i,0,sz) node[N-1+i]=X[i];
        per(i,N-2,0){
            node[i]=Compare(node[i*2+1],node[i*2+2]);
        }
    }
    T Compare(T &A,T &B){
        return std::min(A,B);
    }
    void update(ll X,T val){
        X+=N-1;
        node[X]=val;
        while(X>0){
            X=(X-1)/2;
            node[X]=Compare(node[X*2+1],node[X*2+2]);
        }
    }
    T Query(ll a,ll b,ll now=0,ll l=0,ll r=-1){ //[a,b),[l,r)
        if(r<0) r=N;
        if(r<=a||b<=l) return mem;
        if(a<=l&&r<=b) return node[now];
        auto vl=Query(a,b,now*2+1,l,(l+r)/2),vr=Query(a,b,now*2+2,(l+r)/2,r);
        return Compare(vl,vr);
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
template<typename T>
struct lazy_Segment_tree{
    int N;
    vector<T> node,lazy;
    T INF;
    vector<bool> flag;
    lazy_Segment_tree(vector<T> X,T Y):INF(Y){
        N=1;
        while(X.size()>N) N*=2;
        node.resize(2*N-1,Y);
        lazy.resize(2*N-1);
        flag.resize(2*N-1);
        rep(i,0,X.size()) node[i+N-1]=X[i];
        per(i,N-2,0) node[i]=compare(node[i*2+1],node[i*2+2]);
    }
    T compare(T X,T Y){
        return std::max(X,Y);
    }
    T plus(T X,int l,int r){
        return X;
    }
    void eval(int now,int l,int r){
        if(flag[now]){
            if(r-l>1){
                flag[now*2+1]=flag[now*2+2]=1;
                lazy[now*2+1]=lazy[now*2+2]=lazy[now];
            }
            node[now]=lazy[now];
            flag[now]=0;
        }
    }
    void update(int a,int b,T add,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            lazy[now]=add;
            flag[now]=1;
            eval(now,l,r);
        }
        else{
            update(a,b,add,now*2+1,l,(r+l)/2);
            update(a,b,add,now*2+2,(r+l)/2,r);
            node[now]=compare(node[now*2+1],node[now*2+2]);
        }
    }
    T Query(int a,int b,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return INF;
        if(a<=l&&r<=b) return node[now];
        return compare(Query(a,b,now*2+1,l,(r+l)/2),Query(a,b,now*2+2,(r+l)/2,r));
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        eval(now,l,r);
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
struct Binary_indexed_tree{
    int N;
    vi bit;
    Binary_indexed_tree(int n):N(n){
        bit.resize(N+1,0);
    }
    void add(int x,ll a){
        x++;
        for(x;x<=N;x+=(x&-x)) bit[x]+=a;
    }
    ll sum(int x){
        x++;
        ll ret=0;
        for(x;x>0;x-=(x&-x)) ret+=bit[x];
        return ret;
    }
    ll lower_bound(ll X){
        if(sum(N)<X) return -1;
        ll ret=0,memo=1,sum=0;
        while(memo*2<=N) memo*=2;
        while(memo>0){
            if(memo+ret<=N&&sum+bit[memo+ret]<X){
                sum+=bit[memo+ret];
                ret+=memo;
            }
            memo/=2;
        }
        return ret;
    }
};
struct Union_Find{
    ll N;
    vi par;
    vi siz;
    Union_Find(int n):N(n){
        par.resize(N);
        siz.resize(N,1);
        rep(i,0,N) par[i]=i;
    }
    ll root(ll X){
        if(par[X]==X) return X;
        return par[X]=root(par[X]);
    }
    bool same(ll X,ll Y){
        return root(X)==root(Y);
    }
    void unite(ll X,ll Y){
        X=root(X);
        Y=root(Y);
        if(X==Y) return;
        if(siz[Y]<siz[X]) std::swap(X,Y);
        par[X]=Y;
        siz[Y]+=siz[X];
        siz[X]=0;
    }
    ll size(ll X){
        return siz[root(X)];
    }
};
struct Strongly_Connected_Components{
    ll N,M;
    vii edge,ver;
    vi ind;
    Strongly_Connected_Components(vii e){
        ll n=e.size();
        ll m=0;
        vii revedge(n);
        ind.resize(n);
        rep(i,0,n){
            m+=e[i].size();
            for(auto p:e[i]) revedge[p].pb(i);
        }
        vi num(n,-1),po(n);
        vector<bool> seen(n);
        ll cnt=0;
        rep(i,0,n){
            if(num[i]==-1){
                dfs(i,e,num,cnt);
            }
        }
        rep(i,0,n) po[num[i]]=i;
        per(i,n-1,0){
            ll X=po[i];
            if(!seen[X]){
                std::queue<ll> que;
                vi v;
                que.push(X);
                seen[X]=1;
                while(!que.empty()){
                    ll now=que.front(); que.pop();
                    v.pb(now);
                    ind[now]=ver.size();
                    for(auto p:revedge[now]){
                        if(!seen[p]){
                            seen[p]=1;
                            que.push(p);
                        }
                    }
                }
                ver.pb(v);
            }
        }
        N=ver.size();
        M=0;
        edge.resize(N);
        rep(i,0,n){
            for(auto p:e[i]){
                if(ind[i]==ind[p]) continue;
                M++;
                edge[ind[i]].pb(ind[p]);
            }
        }
    }
    void dfs(ll now,vii &e,vi &num,ll &cnt){
        num[now]=0;
        for(auto next:e[now]){
            if(num[next]==-1){
                dfs(next,e,num,cnt);
            }
        }
        num[now]=cnt++;
    }
};
struct lowlink{
    ll N;
    vii edge;
    vi ord,low,aps;
    vector<bool> used;
    vector<pii> bridges;
    lowlink(vii e):edge(e){
        N=edge.size();
        used.resize(N);
        ord.resize(N);
        low.resize(N);
        ll cnt=0;
        rep(i,0,N){
            if(!used[i]) dfs(i,cnt,-1);
        }
    }
    void dfs(ll now,ll &cnt,ll from){
        used[now]=1;
        ord[now]=cnt++;
        low[now]=ord[now];
        bool flag=0;
        for(auto next:edge[now]){
            if(!used[now]){
                dfs(next,cnt,now);
                low[now]=std::min(low[now],low[next]);
                if(from!=-1&&ord[now]<=low[next]) flag=1;
                if(ord[now]<low[next]) bridges.pb(mp(now,next));
            }
            else if(next!=from) low[now]=std::min(low[now],ord[next]);
        }
        if(from==-1&&edge[now].size()>=2) flag=1;
        if(flag) aps.pb(now);
    }
};
struct Directed_Gragh{
    ll N,M;
    vii edge;
    Directed_Gragh(vii e):edge(e){
        N=edge.size();
        rep(i,0,N) M+=edge[i].size();
    }
    vi sort(){
        vi ret;
        vi cnt(N);
        rep(i,0,N){
            for(auto p:edge[i]) cnt[p]++;
        }
        std::queue<ll> que;
        rep(i,0,N){
            if(cnt[i]==0) que.push(i);
        }
        while(!que.empty()){
            ll now=que.front(); que.pop();
            ret.pb(now);
            for(auto next:edge[now]){
                cnt[next]--;
                if(cnt[next]==0) que.push(next);
            }
        }
        return ret;
    }
};
struct Tree{
    int N;
    vii dp;
    vi par;
    vi dist;
    vi subtree;
    vii edge;
    Tree(vii E):edge(E){
        N=edge.size();
        dp.resize(N);
        par.resize(N);
        dist.resize(N,-1);
        for(int i=0;i<N;i++) dp[i].resize(30);
        dist[0]=dp[0][0]=0;
        std::queue<int> que;
        que.push(0);
        while(!que.empty()){
            int now=que.front(); que.pop();
            for(int i=0;i<edge[now].size();i++){
                int next=edge[now][i];
                if(dist[next]==-1){
                    dist[next]=dist[now]+1;
                    que.push(next);
                    par[next]=now;
                    dp[next][0]=now;
                }
            }
        }
        for(int i=1;i<30;i++){
            for(int j=0;j<N;j++) dp[j][i]=dp[dp[j][i-1]][i-1];
        }
    }
    int LCA(int X,int Y){
        if(dist[X]<dist[Y]) std::swap(X,Y);
        {
            int Z=dist[X]-dist[Y];
            for(int i=0;i<30;i++){
                if(Z&(1<<i)){
                    X=dp[X][i];
                }
            }
        }
        if(X==Y) return X;
        for(int i=29;i>=0;i--){
            if(dp[X][i]!=dp[Y][i]){
                X=dp[X][i];
                Y=dp[Y][i];
            }
        }
        return dp[X][0];
    }
    void Subtree(){
        subtree.resize(N,-1);
        dfs(0);
    }
    void dfs(ll now){
        subtree[now]=1;
        for(auto next:edge[now]){
            if(subtree[next]==-1){
                dfs(next);
                subtree[now]+=subtree[next];
            }
        }
    }
};
template<typename T>
vi Declination_sort(ll N,T x,T y,vi X,vi Y){
    vi ret(N);
    vector<std::pair<long double,ll>> data(N);
    rep(i,0,N){
        long double A=X[i]-x,B=Y[i]-y;
        long double rad=atan2(B,A);
        data[i]=mp(rad,i);
    }
    sort(ALL(data));
    rep(i,0,N) ret[i]=data[i].second;
    return ret;
}
struct Convex_Hull{
    ll N;
    vi X,Y;
    Convex_Hull(vi A,vi B):X(A),Y(B){
        N=X.size();
        ll sx=0,sy=0;
        rep(i,0,N){
            sx+=X[i];
            sy+=Y[i];
        }
        double x=sx*1.0/N,y=sy*1.0/N;
        vi ind=Declination_sort<long double>(N,x,y,X,Y);
        vi C(N),D(N);
        rep(i,0,N){
            C[i]=X[ind[i]];
            D[i]=Y[ind[i]];
        }
        X=C;
        Y=D;
    }
    /*vi Find(){

    }*/
};
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
vi fac,finv,inv;
void COMinit() {
    fac.resize(MAX);
    finv.resize(MAX);
    inv.resize(MAX);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll COM(ll n,ll r){
    if(n<r||n<0||r<0) return 0;
    return fac[n]*finv[r]%MOD*finv[n-r]%MOD;
}
void comp(vi &A){
    std::map<ll,ll> memo;
    rep(i,0,A.size()) memo[A[i]]=0;
    ll cnt=0;
    for(auto &p:memo) p.second=cnt++;
    rep(i,0,A.size()) A[i]=memo[A[i]];
}
void dec(std::map<ll,ll> &mem,ll X){
    mem[X]--;
    if(mem[X]==0) mem.erase(X);
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::random_device rnd;
    std::mt19937 mt(rnd());
    ll N,M; cin>>N>>M;
    vi A(N),B(N);
    rep(i,0,N) cin>>A[i]>>B[i];
    vector<pii> data(M);
    rep(i,0,M) cin>>data[i].first>>data[i].second;
    sort(ALL(data));
    {
        std::map<ll,ll> a;
        rep(i,0,M) a[data[i].first]=data[i].second;
        data.clear();
        for(auto p:a) data.pb(p);
        M=a.size();
    }
    std::map<ll,ll> cnt;
    vi mem(M);
    rep(i,0,M) cnt[data[i].second]++;
    rep(i,0,M){
        mem[i]=(*cnt.rbegin()).first+1;
        dec(cnt,data[i].second);
    }
    ll ans=inf;
    const ll max=1e6+10;
    vector<vector<pii>> record(max+1);
    rep(i,0,N){
        ll min=inf;
        rep(j,0,M){
            if(data[j].first>=A[i]){
                ll X=data[j].first-A[i];
                ll Y=0;
                if(j) Y=std::max(0ll,data[j-1].first+1-A[i]);
                ll Z=std::max(0ll,mem[j]-B[i]);
                record[Y].pb(mp(Z,X));
            }
        }
        ll X=std::max(0ll,data[M-1].first+1-A[i]);
        record[X].pb(mp(0,inf));
    }
    cnt.clear();
    std::priority_queue<pii> que;
    REP(i,0,max){
        ll max=inf;
        for(auto p:record[i]) que.push(p);
        while(!que.empty()){
            auto p=que.top();
            if(p.second>=i){
                max=p.first; break;
            }
            que.pop();
        }
        //cout<<i<<" "<<i+(*rbegin(cnt)).first<<" "<<record[i].size()<<endl;
        ans=std::min(ans,i+max);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
//using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i,j,n) for(ll i=(ll)(j);i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(ll)(j);i<=(ll)(n);i++)
#define per(i,j,n) for(ll i=(ll)(j);(ll)(n)<=i;i--)
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(ll)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
//using std::endl;
using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::upper_bound;
using std::lower_bound;
using vi=vector<ll>;
using vii=vector<vi>;
using pii=std::pair<ll,ll>;
//
constexpr ll MOD=1e9+7;
//constexpr ll MOD=998244353;
//constexpr ll MOD=10000000;
//constexpr ll MOD=1e4;
//constexpr ll MOD=1e5;
constexpr ll MAX=3e6;
constexpr ll inf=(1ll<<60);
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template<typename T>
struct Segment_tree{
    ll N;
    T mem;
    vector<T> node;
    Segment_tree(vector<T> &X,T m):mem(m){
        ll sz=X.size();
        N=1;
        while(N<sz) N*=2;
        node.resize(2*N-1,mem);
        rep(i,0,sz) node[N-1+i]=X[i];
        per(i,N-2,0){
            node[i]=Compare(node[i*2+1],node[i*2+2]);
        }
    }
    T Compare(T &A,T &B){
        return std::min(A,B);
    }
    void update(ll X,T val){
        X+=N-1;
        node[X]=val;
        while(X>0){
            X=(X-1)/2;
            node[X]=Compare(node[X*2+1],node[X*2+2]);
        }
    }
    T Query(ll a,ll b,ll now=0,ll l=0,ll r=-1){ //[a,b),[l,r)
        if(r<0) r=N;
        if(r<=a||b<=l) return mem;
        if(a<=l&&r<=b) return node[now];
        auto vl=Query(a,b,now*2+1,l,(l+r)/2),vr=Query(a,b,now*2+2,(l+r)/2,r);
        return Compare(vl,vr);
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
template<typename T>
struct lazy_Segment_tree{
    int N;
    vector<T> node,lazy;
    T INF;
    vector<bool> flag;
    lazy_Segment_tree(vector<T> X,T Y):INF(Y){
        N=1;
        while(X.size()>N) N*=2;
        node.resize(2*N-1,Y);
        lazy.resize(2*N-1);
        flag.resize(2*N-1);
        rep(i,0,X.size()) node[i+N-1]=X[i];
        per(i,N-2,0) node[i]=compare(node[i*2+1],node[i*2+2]);
    }
    T compare(T X,T Y){
        return std::max(X,Y);
    }
    T plus(T X,int l,int r){
        return X;
    }
    void eval(int now,int l,int r){
        if(flag[now]){
            if(r-l>1){
                flag[now*2+1]=flag[now*2+2]=1;
                lazy[now*2+1]=lazy[now*2+2]=lazy[now];
            }
            node[now]=lazy[now];
            flag[now]=0;
        }
    }
    void update(int a,int b,T add,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            lazy[now]=add;
            flag[now]=1;
            eval(now,l,r);
        }
        else{
            update(a,b,add,now*2+1,l,(r+l)/2);
            update(a,b,add,now*2+2,(r+l)/2,r);
            node[now]=compare(node[now*2+1],node[now*2+2]);
        }
    }
    T Query(int a,int b,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return INF;
        if(a<=l&&r<=b) return node[now];
        return compare(Query(a,b,now*2+1,l,(r+l)/2),Query(a,b,now*2+2,(r+l)/2,r));
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        eval(now,l,r);
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
struct Binary_indexed_tree{
    int N;
    vi bit;
    Binary_indexed_tree(int n):N(n){
        bit.resize(N+1,0);
    }
    void add(int x,ll a){
        x++;
        for(x;x<=N;x+=(x&-x)) bit[x]+=a;
    }
    ll sum(int x){
        x++;
        ll ret=0;
        for(x;x>0;x-=(x&-x)) ret+=bit[x];
        return ret;
    }
    ll lower_bound(ll X){
        if(sum(N)<X) return -1;
        ll ret=0,memo=1,sum=0;
        while(memo*2<=N) memo*=2;
        while(memo>0){
            if(memo+ret<=N&&sum+bit[memo+ret]<X){
                sum+=bit[memo+ret];
                ret+=memo;
            }
            memo/=2;
        }
        return ret;
    }
};
struct Union_Find{
    ll N;
    vi par;
    vi siz;
    Union_Find(int n):N(n){
        par.resize(N);
        siz.resize(N,1);
        rep(i,0,N) par[i]=i;
    }
    ll root(ll X){
        if(par[X]==X) return X;
        return par[X]=root(par[X]);
    }
    bool same(ll X,ll Y){
        return root(X)==root(Y);
    }
    void unite(ll X,ll Y){
        X=root(X);
        Y=root(Y);
        if(X==Y) return;
        if(siz[Y]<siz[X]) std::swap(X,Y);
        par[X]=Y;
        siz[Y]+=siz[X];
        siz[X]=0;
    }
    ll size(ll X){
        return siz[root(X)];
    }
};
struct Strongly_Connected_Components{
    ll N,M;
    vii edge,ver;
    vi ind;
    Strongly_Connected_Components(vii e){
        ll n=e.size();
        ll m=0;
        vii revedge(n);
        ind.resize(n);
        rep(i,0,n){
            m+=e[i].size();
            for(auto p:e[i]) revedge[p].pb(i);
        }
        vi num(n,-1),po(n);
        vector<bool> seen(n);
        ll cnt=0;
        rep(i,0,n){
            if(num[i]==-1){
                dfs(i,e,num,cnt);
            }
        }
        rep(i,0,n) po[num[i]]=i;
        per(i,n-1,0){
            ll X=po[i];
            if(!seen[X]){
                std::queue<ll> que;
                vi v;
                que.push(X);
                seen[X]=1;
                while(!que.empty()){
                    ll now=que.front(); que.pop();
                    v.pb(now);
                    ind[now]=ver.size();
                    for(auto p:revedge[now]){
                        if(!seen[p]){
                            seen[p]=1;
                            que.push(p);
                        }
                    }
                }
                ver.pb(v);
            }
        }
        N=ver.size();
        M=0;
        edge.resize(N);
        rep(i,0,n){
            for(auto p:e[i]){
                if(ind[i]==ind[p]) continue;
                M++;
                edge[ind[i]].pb(ind[p]);
            }
        }
    }
    void dfs(ll now,vii &e,vi &num,ll &cnt){
        num[now]=0;
        for(auto next:e[now]){
            if(num[next]==-1){
                dfs(next,e,num,cnt);
            }
        }
        num[now]=cnt++;
    }
};
struct lowlink{
    ll N;
    vii edge;
    vi ord,low,aps;
    vector<bool> used;
    vector<pii> bridges;
    lowlink(vii e):edge(e){
        N=edge.size();
        used.resize(N);
        ord.resize(N);
        low.resize(N);
        ll cnt=0;
        rep(i,0,N){
            if(!used[i]) dfs(i,cnt,-1);
        }
    }
    void dfs(ll now,ll &cnt,ll from){
        used[now]=1;
        ord[now]=cnt++;
        low[now]=ord[now];
        bool flag=0;
        for(auto next:edge[now]){
            if(!used[now]){
                dfs(next,cnt,now);
                low[now]=std::min(low[now],low[next]);
                if(from!=-1&&ord[now]<=low[next]) flag=1;
                if(ord[now]<low[next]) bridges.pb(mp(now,next));
            }
            else if(next!=from) low[now]=std::min(low[now],ord[next]);
        }
        if(from==-1&&edge[now].size()>=2) flag=1;
        if(flag) aps.pb(now);
    }
};
struct Directed_Gragh{
    ll N,M;
    vii edge;
    Directed_Gragh(vii e):edge(e){
        N=edge.size();
        rep(i,0,N) M+=edge[i].size();
    }
    vi sort(){
        vi ret;
        vi cnt(N);
        rep(i,0,N){
            for(auto p:edge[i]) cnt[p]++;
        }
        std::queue<ll> que;
        rep(i,0,N){
            if(cnt[i]==0) que.push(i);
        }
        while(!que.empty()){
            ll now=que.front(); que.pop();
            ret.pb(now);
            for(auto next:edge[now]){
                cnt[next]--;
                if(cnt[next]==0) que.push(next);
            }
        }
        return ret;
    }
};
struct Tree{
    int N;
    vii dp;
    vi par;
    vi dist;
    vi subtree;
    vii edge;
    Tree(vii E):edge(E){
        N=edge.size();
        dp.resize(N);
        par.resize(N);
        dist.resize(N,-1);
        for(int i=0;i<N;i++) dp[i].resize(30);
        dist[0]=dp[0][0]=0;
        std::queue<int> que;
        que.push(0);
        while(!que.empty()){
            int now=que.front(); que.pop();
            for(int i=0;i<edge[now].size();i++){
                int next=edge[now][i];
                if(dist[next]==-1){
                    dist[next]=dist[now]+1;
                    que.push(next);
                    par[next]=now;
                    dp[next][0]=now;
                }
            }
        }
        for(int i=1;i<30;i++){
            for(int j=0;j<N;j++) dp[j][i]=dp[dp[j][i-1]][i-1];
        }
    }
    int LCA(int X,int Y){
        if(dist[X]<dist[Y]) std::swap(X,Y);
        {
            int Z=dist[X]-dist[Y];
            for(int i=0;i<30;i++){
                if(Z&(1<<i)){
                    X=dp[X][i];
                }
            }
        }
        if(X==Y) return X;
        for(int i=29;i>=0;i--){
            if(dp[X][i]!=dp[Y][i]){
                X=dp[X][i];
                Y=dp[Y][i];
            }
        }
        return dp[X][0];
    }
    void Subtree(){
        subtree.resize(N,-1);
        dfs(0);
    }
    void dfs(ll now){
        subtree[now]=1;
        for(auto next:edge[now]){
            if(subtree[next]==-1){
                dfs(next);
                subtree[now]+=subtree[next];
            }
        }
    }
};
template<typename T>
vi Declination_sort(ll N,T x,T y,vi X,vi Y){
    vi ret(N);
    vector<std::pair<long double,ll>> data(N);
    rep(i,0,N){
        long double A=X[i]-x,B=Y[i]-y;
        long double rad=atan2(B,A);
        data[i]=mp(rad,i);
    }
    sort(ALL(data));
    rep(i,0,N) ret[i]=data[i].second;
    return ret;
}
struct Convex_Hull{
    ll N;
    vi X,Y;
    Convex_Hull(vi A,vi B):X(A),Y(B){
        N=X.size();
        ll sx=0,sy=0;
        rep(i,0,N){
            sx+=X[i];
            sy+=Y[i];
        }
        double x=sx*1.0/N,y=sy*1.0/N;
        vi ind=Declination_sort<long double>(N,x,y,X,Y);
        vi C(N),D(N);
        rep(i,0,N){
            C[i]=X[ind[i]];
            D[i]=Y[ind[i]];
        }
        X=C;
        Y=D;
    }
    /*vi Find(){

    }*/
};
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
vi fac,finv,inv;
void COMinit() {
    fac.resize(MAX);
    finv.resize(MAX);
    inv.resize(MAX);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll COM(ll n,ll r){
    if(n<r||n<0||r<0) return 0;
    return fac[n]*finv[r]%MOD*finv[n-r]%MOD;
}
void comp(vi &A){
    std::map<ll,ll> memo;
    rep(i,0,A.size()) memo[A[i]]=0;
    ll cnt=0;
    for(auto &p:memo) p.second=cnt++;
    rep(i,0,A.size()) A[i]=memo[A[i]];
}
void dec(std::map<ll,ll> &mem,ll X){
    mem[X]--;
    if(mem[X]==0) mem.erase(X);
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::random_device rnd;
    std::mt19937 mt(rnd());
    ll N,M; cin>>N>>M;
    vi A(N),B(N);
    rep(i,0,N) cin>>A[i]>>B[i];
    vector<pii> data(M);
    rep(i,0,M) cin>>data[i].first>>data[i].second;
    sort(ALL(data));
    {
        std::map<ll,ll> a;
        rep(i,0,M) a[data[i].first]=data[i].second;
        data.clear();
        for(auto p:a) data.pb(p);
        M=a.size();
    }
    std::map<ll,ll> cnt;
    vi mem(M);
    rep(i,0,M) cnt[data[i].second]++;
    rep(i,0,M){
        mem[i]=(*cnt.rbegin()).first+1;
        dec(cnt,data[i].second);
    }
    ll ans=inf;
    const ll max=1e6+10;
    vii record(max+1);
    rep(i,0,N){
        ll min=inf;
        rep(j,0,M){
            if(data[j].first>=A[i]){
                ll X=data[j].first-A[i];
                ll Y=0;
                if(j) Y=std::max(0ll,data[j-1].first+1-A[i]);
                ll Z=std::max(0ll,mem[j]-B[i]);
                record[X+1].pb(inf+Z);
                record[Y].pb(Z);
            }
        }
        ll X=std::max(0ll,data[M-1].first+1-A[i]);
        record[X].pb(0);
    }
    cnt.clear();
    REP(i,0,max){
        for(auto p:record[i]){
            if(p>=inf) dec(cnt,p-inf);
            else cnt[p]++;
        }
        //cout<<i<<" "<<i+(*rbegin(cnt)).first<<" "<<record[i].size()<<endl;
        ans=std::min(ans,i+(*rbegin(cnt)).first);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
//using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i,j,n) for(ll i=(ll)(j);i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(ll)(j);i<=(ll)(n);i++)
#define per(i,j,n) for(ll i=(ll)(j);(ll)(n)<=i;i--)
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(ll)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
//using std::endl;
using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::upper_bound;
using std::lower_bound;
using vi=vector<ll>;
using vii=vector<vi>;
using pii=std::pair<ll,ll>;
//
constexpr ll MOD=1e9+7;
//constexpr ll MOD=998244353;
//constexpr ll MOD=10000000;
//constexpr ll MOD=1e4;
//constexpr ll MOD=1e5;
constexpr ll MAX=3e6;
constexpr ll inf=(1ll<<60);
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template<typename T>
struct Segment_tree{
    ll N;
    T mem;
    vector<T> node;
    Segment_tree(vector<T> &X,T m):mem(m){
        ll sz=X.size();
        N=1;
        while(N<sz) N*=2;
        node.resize(2*N-1,mem);
        rep(i,0,sz) node[N-1+i]=X[i];
        per(i,N-2,0){
            node[i]=Compare(node[i*2+1],node[i*2+2]);
        }
    }
    T Compare(T &A,T &B){
        return std::min(A,B);
    }
    void update(ll X,T val){
        X+=N-1;
        node[X]=val;
        while(X>0){
            X=(X-1)/2;
            node[X]=Compare(node[X*2+1],node[X*2+2]);
        }
    }
    T Query(ll a,ll b,ll now=0,ll l=0,ll r=-1){ //[a,b),[l,r)
        if(r<0) r=N;
        if(r<=a||b<=l) return mem;
        if(a<=l&&r<=b) return node[now];
        auto vl=Query(a,b,now*2+1,l,(l+r)/2),vr=Query(a,b,now*2+2,(l+r)/2,r);
        return Compare(vl,vr);
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
template<typename T>
struct lazy_Segment_tree{
    int N;
    vector<T> node,lazy;
    T INF;
    vector<bool> flag;
    lazy_Segment_tree(vector<T> X,T Y):INF(Y){
        N=1;
        while(X.size()>N) N*=2;
        node.resize(2*N-1,Y);
        lazy.resize(2*N-1);
        flag.resize(2*N-1);
        rep(i,0,X.size()) node[i+N-1]=X[i];
        per(i,N-2,0) node[i]=compare(node[i*2+1],node[i*2+2]);
    }
    T compare(T X,T Y){
        return std::max(X,Y);
    }
    T plus(T X,int l,int r){
        return X;
    }
    void eval(int now,int l,int r){
        if(flag[now]){
            if(r-l>1){
                flag[now*2+1]=flag[now*2+2]=1;
                lazy[now*2+1]=lazy[now*2+2]=lazy[now];
            }
            node[now]=lazy[now];
            flag[now]=0;
        }
    }
    void update(int a,int b,T add,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            lazy[now]=add;
            flag[now]=1;
            eval(now,l,r);
        }
        else{
            update(a,b,add,now*2+1,l,(r+l)/2);
            update(a,b,add,now*2+2,(r+l)/2,r);
            node[now]=compare(node[now*2+1],node[now*2+2]);
        }
    }
    T Query(int a,int b,int now=0,int l=0,int r=-1){
        if(r<0) r=N;
        eval(now,l,r);
        if(b<=l||r<=a) return INF;
        if(a<=l&&r<=b) return node[now];
        return compare(Query(a,b,now*2+1,l,(r+l)/2),Query(a,b,now*2+2,(r+l)/2,r));
    }
    ll lower_bound(ll left,ll right,T val,ll now=0,ll l=0,ll r=-1){
        eval(now,l,r);
        if(r<0) r=N;
        if(node[now]<val||l>=right||left>=r) return r;
        else if(now>=N-1) return l;
        ll vl=lower_bound(left,right,val,now*2+1,l,(l+r)/2);
        if(vl==(l+r)/2) return lower_bound(left,right,val,now*2+2,(l+r)/2,r);
        return vl;
    }
};
struct Binary_indexed_tree{
    int N;
    vi bit;
    Binary_indexed_tree(int n):N(n){
        bit.resize(N+1,0);
    }
    void add(int x,ll a){
        x++;
        for(x;x<=N;x+=(x&-x)) bit[x]+=a;
    }
    ll sum(int x){
        x++;
        ll ret=0;
        for(x;x>0;x-=(x&-x)) ret+=bit[x];
        return ret;
    }
    ll lower_bound(ll X){
        if(sum(N)<X) return -1;
        ll ret=0,memo=1,sum=0;
        while(memo*2<=N) memo*=2;
        while(memo>0){
            if(memo+ret<=N&&sum+bit[memo+ret]<X){
                sum+=bit[memo+ret];
                ret+=memo;
            }
            memo/=2;
        }
        return ret;
    }
};
struct Union_Find{
    ll N;
    vi par;
    vi siz;
    Union_Find(int n):N(n){
        par.resize(N);
        siz.resize(N,1);
        rep(i,0,N) par[i]=i;
    }
    ll root(ll X){
        if(par[X]==X) return X;
        return par[X]=root(par[X]);
    }
    bool same(ll X,ll Y){
        return root(X)==root(Y);
    }
    void unite(ll X,ll Y){
        X=root(X);
        Y=root(Y);
        if(X==Y) return;
        if(siz[Y]<siz[X]) std::swap(X,Y);
        par[X]=Y;
        siz[Y]+=siz[X];
        siz[X]=0;
    }
    ll size(ll X){
        return siz[root(X)];
    }
};
struct Strongly_Connected_Components{
    ll N,M;
    vii edge,ver;
    vi ind;
    Strongly_Connected_Components(vii e){
        ll n=e.size();
        ll m=0;
        vii revedge(n);
        ind.resize(n);
        rep(i,0,n){
            m+=e[i].size();
            for(auto p:e[i]) revedge[p].pb(i);
        }
        vi num(n,-1),po(n);
        vector<bool> seen(n);
        ll cnt=0;
        rep(i,0,n){
            if(num[i]==-1){
                dfs(i,e,num,cnt);
            }
        }
        rep(i,0,n) po[num[i]]=i;
        per(i,n-1,0){
            ll X=po[i];
            if(!seen[X]){
                std::queue<ll> que;
                vi v;
                que.push(X);
                seen[X]=1;
                while(!que.empty()){
                    ll now=que.front(); que.pop();
                    v.pb(now);
                    ind[now]=ver.size();
                    for(auto p:revedge[now]){
                        if(!seen[p]){
                            seen[p]=1;
                            que.push(p);
                        }
                    }
                }
                ver.pb(v);
            }
        }
        N=ver.size();
        M=0;
        edge.resize(N);
        rep(i,0,n){
            for(auto p:e[i]){
                if(ind[i]==ind[p]) continue;
                M++;
                edge[ind[i]].pb(ind[p]);
            }
        }
    }
    void dfs(ll now,vii &e,vi &num,ll &cnt){
        num[now]=0;
        for(auto next:e[now]){
            if(num[next]==-1){
                dfs(next,e,num,cnt);
            }
        }
        num[now]=cnt++;
    }
};
struct lowlink{
    ll N;
    vii edge;
    vi ord,low,aps;
    vector<bool> used;
    vector<pii> bridges;
    lowlink(vii e):edge(e){
        N=edge.size();
        used.resize(N);
        ord.resize(N);
        low.resize(N);
        ll cnt=0;
        rep(i,0,N){
            if(!used[i]) dfs(i,cnt,-1);
        }
    }
    void dfs(ll now,ll &cnt,ll from){
        used[now]=1;
        ord[now]=cnt++;
        low[now]=ord[now];
        bool flag=0;
        for(auto next:edge[now]){
            if(!used[now]){
                dfs(next,cnt,now);
                low[now]=std::min(low[now],low[next]);
                if(from!=-1&&ord[now]<=low[next]) flag=1;
                if(ord[now]<low[next]) bridges.pb(mp(now,next));
            }
            else if(next!=from) low[now]=std::min(low[now],ord[next]);
        }
        if(from==-1&&edge[now].size()>=2) flag=1;
        if(flag) aps.pb(now);
    }
};
struct Directed_Gragh{
    ll N,M;
    vii edge;
    Directed_Gragh(vii e):edge(e){
        N=edge.size();
        rep(i,0,N) M+=edge[i].size();
    }
    vi sort(){
        vi ret;
        vi cnt(N);
        rep(i,0,N){
            for(auto p:edge[i]) cnt[p]++;
        }
        std::queue<ll> que;
        rep(i,0,N){
            if(cnt[i]==0) que.push(i);
        }
        while(!que.empty()){
            ll now=que.front(); que.pop();
            ret.pb(now);
            for(auto next:edge[now]){
                cnt[next]--;
                if(cnt[next]==0) que.push(next);
            }
        }
        return ret;
    }
};
struct Tree{
    int N;
    vii dp;
    vi par;
    vi dist;
    vi subtree;
    vii edge;
    Tree(vii E):edge(E){
        N=edge.size();
        dp.resize(N);
        par.resize(N);
        dist.resize(N,-1);
        for(int i=0;i<N;i++) dp[i].resize(30);
        dist[0]=dp[0][0]=0;
        std::queue<int> que;
        que.push(0);
        while(!que.empty()){
            int now=que.front(); que.pop();
            for(int i=0;i<edge[now].size();i++){
                int next=edge[now][i];
                if(dist[next]==-1){
                    dist[next]=dist[now]+1;
                    que.push(next);
                    par[next]=now;
                    dp[next][0]=now;
                }
            }
        }
        for(int i=1;i<30;i++){
            for(int j=0;j<N;j++) dp[j][i]=dp[dp[j][i-1]][i-1];
        }
    }
    int LCA(int X,int Y){
        if(dist[X]<dist[Y]) std::swap(X,Y);
        {
            int Z=dist[X]-dist[Y];
            for(int i=0;i<30;i++){
                if(Z&(1<<i)){
                    X=dp[X][i];
                }
            }
        }
        if(X==Y) return X;
        for(int i=29;i>=0;i--){
            if(dp[X][i]!=dp[Y][i]){
                X=dp[X][i];
                Y=dp[Y][i];
            }
        }
        return dp[X][0];
    }
    void Subtree(){
        subtree.resize(N,-1);
        dfs(0);
    }
    void dfs(ll now){
        subtree[now]=1;
        for(auto next:edge[now]){
            if(subtree[next]==-1){
                dfs(next);
                subtree[now]+=subtree[next];
            }
        }
    }
};
template<typename T>
vi Declination_sort(ll N,T x,T y,vi X,vi Y){
    vi ret(N);
    vector<std::pair<long double,ll>> data(N);
    rep(i,0,N){
        long double A=X[i]-x,B=Y[i]-y;
        long double rad=atan2(B,A);
        data[i]=mp(rad,i);
    }
    sort(ALL(data));
    rep(i,0,N) ret[i]=data[i].second;
    return ret;
}
struct Convex_Hull{
    ll N;
    vi X,Y;
    Convex_Hull(vi A,vi B):X(A),Y(B){
        N=X.size();
        ll sx=0,sy=0;
        rep(i,0,N){
            sx+=X[i];
            sy+=Y[i];
        }
        double x=sx*1.0/N,y=sy*1.0/N;
        vi ind=Declination_sort<long double>(N,x,y,X,Y);
        vi C(N),D(N);
        rep(i,0,N){
            C[i]=X[ind[i]];
            D[i]=Y[ind[i]];
        }
        X=C;
        Y=D;
    }
    /*vi Find(){

    }*/
};
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
vi fac,finv,inv;
void COMinit() {
    fac.resize(MAX);
    finv.resize(MAX);
    inv.resize(MAX);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll COM(ll n,ll r){
    if(n<r||n<0||r<0) return 0;
    return fac[n]*finv[r]%MOD*finv[n-r]%MOD;
}
void comp(vi &A){
    std::map<ll,ll> memo;
    rep(i,0,A.size()) memo[A[i]]=0;
    ll cnt=0;
    for(auto &p:memo) p.second=cnt++;
    rep(i,0,A.size()) A[i]=memo[A[i]];
}
void dec(std::map<ll,ll> &mem,ll X){
    mem[X]--;
    if(mem[X]==0) mem.erase(X);
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::random_device rnd;
    std::mt19937 mt(rnd());
    ll N,M; cin>>N>>M;
    vi A(N),B(N);
    rep(i,0,N) cin>>A[i]>>B[i];
    vector<pii> data(M);
    rep(i,0,M) cin>>data[i].first>>data[i].second;
    sort(ALL(data));
    {
        std::map<ll,ll> a;
        rep(i,0,M) a[data[i].first]=data[i].second;
        data.clear();
        for(auto p:a) data.pb(p);
        M=a.size();
    }
    std::map<ll,ll> cnt;
    vi mem(M);
    rep(i,0,M) cnt[data[i].second]++;
    rep(i,0,M){
        mem[i]=(*cnt.rbegin()).first+1;
        dec(cnt,data[i].second);
    }
    ll ans=inf;
    const ll max=1e6+10;
    vector<vector<pii>> record(max+1);
    rep(i,0,N){
        ll min=inf;
        rep(j,0,M){
            if(data[j].first>=A[i]){
                ll X=data[j].first-A[i];
                ll Y=0;
                if(j) Y=std::max(0ll,data[j-1].first+1-A[i]);
                ll Z=std::max(0ll,mem[j]-B[i]);
                record[Y].pb(mp(Z,X));
            }
        }
    }
    cnt.clear();
    std::priority_queue<pii> que;
    rep(i,0,max){
        ll max=0;
        for(auto p:record[i]){
            que.push(p);
        }
        while(!que.empty()){
            auto p=que.top();
            if(p.second>=i){
                max=p.first; break;
            }
            que.pop();
        }
        //cout<<i<<" "<<i+(*rbegin(cnt)).first<<" "<<record[i].size()<<endl;
        ans=std::min(ans,i+max);
    }
    cout<<ans<<endl;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], s[i][1]);
  vector<vector<int>> ord(1e6+5);
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    for(int j = m-1; j >= 0 && x <= s[j][0]; j --){
      int d = max(0, mh[j]-y+1);
      int px = (j?max(x,s[j-1][0]+1):x);
      ord[px-x].push_back(d);
      ord[s[j][0]-x+1].push_back(-d);
    }
    ord[max(0,s[m-1][0]-x+1)].push_back(0);
  }
  int ans = 1e7;
  multiset<int> act; act.insert(0);
  for(int i = 0; i < ord.size(); i ++){
    for(auto&j:ord[i]){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    ans = min(ans, i+*act.rbegin());
  }
  cout << ans;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], s[i][1]);
  map<int, vector<int>> ord;
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    int px = x-1;
    for(int j = 0; j < m; j ++){
      if(x <= s[j][0]){
        int d = max(0, mh[j]-y+1);
        ord[px-x+1].push_back(d);
        px = s[j][0];
        ord[px-x+1].push_back(-d);
      }
    }
    if(x <= s[m-1][0]) ord[s[m-1][0]-x+1].push_back(0);
  }
  int ans = 1e7;
  multiset<int> act; act.insert(0);
  for(auto&i:ord){
    int dx = i.first;
    for(auto&j:i.second){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    // cout << dx << ", " << *act.rbegin() << endl;
    if(act.size()) ans = min(ans, dx+*act.rbegin());
  }
  cout << ans;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], s[i][1]);
  map<int, vector<int>> ord;
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    int px = x-1;
    for(int j = 0; j < m; j ++){
      if(x <= s[j][0]){
        int d = max(0, mh[j]-y+1);
        ord[px-x+1].push_back(d);
        px = s[j][0];
        ord[px-x+1].push_back(-d);
      }
    }
    if(x <= s[m-1][0]) ord[s[m-1][0]-x+1].push_back(0);
    else ord[0].push_back(0);
  }
  int ans = 1e7;
  multiset<int> act; act.insert(0);
  for(auto&i:ord){
    int dx = i.first;
    for(auto&j:i.second){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    // cout << dx << ", " << *act.rbegin() << endl;
    if(act.size()) ans = min(ans, dx+*act.rbegin());
  }
  cout << ans;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], s[i][1]);
  map<int, vector<int>> ord;
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    int px = x-1;
    for(int j = 0; j < m; j ++){
      if(x <= s[j][0]){
        int d = max(0, mh[j]-y+1);
        ord[px-x+1].push_back(d);
        px = s[j][0];
        ord[px-x+1].push_back(-d);
      }
    }
    if(x <= s[m-1][0]) ord[s[m-1][0]-x+1].push_back(0);
  }
  int ans = 2e6;
  multiset<int> act; act.insert(0);
  for(auto&i:ord){
    int dx = i.first;
    for(auto&j:i.second){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    // cout << dx << ", " << *act.rbegin() << endl;
    if(act.size()) ans = min(ans, dx+*act.rbegin());
  }
  cout << ans;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  vector<int> f(m); f[m-1] = 1;
  for(int i = m-2; i >= 0; i --) if(s[i][1] > mh[i])
    mh[i] = s[i][1], f[i] = 1;
  vector<int> ns; for(int i = 0; i < m; i ++) if(f[i]) ns.push_back(i);
  vector<vector<int>> ord(1e6+5);
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    int px = x-1;
    for(auto&j:ns){
      if(x <= s[j][0]){
        int d = max(0, mh[j]-y+1);
        ord[px-x+1].push_back(d);
        px = s[j][0];
        ord[px-x+1].push_back(-d);
      }
    }
    if(x <= s[m-1][0]) ord[s[m-1][0]-x+1].push_back(0);
    else ord[0].push_back(0);
  }
  int ans = 1e7;
  multiset<int> act; act.insert(0);
  for(int i = 0; i < ord.size(); i ++){
    for(auto&j:ord[i]){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    ans = min(ans, i+*act.rbegin());
  }
  cout << ans;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  vector<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.push_back({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.push_back({x,y});
  }
  sort(r.begin(), r.end());
  sort(s.begin(), s.end());
  vector<int> mh(m); mh[m-1] = s[m-1][1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], s[i][1]);
  vector<int> ord(1e6+5);
  for(int i = 0; i < n; i ++){
    int x = r[i][0], y = r[i][1];
    for(int j = m-1; j >= 0 && x <= s[j][0]; j --){
      int d = max(0, mh[j]-y+1);
      int px = (j?max(x,s[j-1][0]+1):x);
      ord[s[j][0]-x] = max(ord[s[j][0]-x], d);
    }
  }
  int ans = 1e7;
  int dy = 0;
  for(int i = ord.size()-1; i >= 0; i --){
    dy = max(dy, ord[i]);
    ans = min(ans, i+dy);
  }
  cout << ans;
}


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

void filter(set<vector<int>>&c){
  auto t = c.begin();
  while(1){
    auto t2 = t; ++ t2;
    if(t2 == c.end()) break;
    if((*t)[1] <= (*t2)[1]) c.erase(t);
    t = t2;
  }
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m; cin >> n >> m;
  set<vector<int>> r,s;
  int x, y;
  for(int i = 0; i < n; i ++){
    cin >> x >> y;
    r.insert({x,y});
  }
  for(int i = 0; i < m; i ++){
    cin >> x >> y;
    s.insert({x,y});
  }
  filter(r); filter(s);
  n = r.size(), m = s.size();
  auto t = --s.end();
  vector<int> mh(m); mh[m-1] = (*t)[1];
  for(int i = m-2; i >= 0; i --) mh[i] = max(mh[i+1], (*--t)[1]);
  vector<vector<int>> ord(1e6+5);
  for(auto&i:r){
    int x = i[0], y = i[1];
    int px = x;
    for(auto&j:s){
      if(x <= j[0]){
        int d = max(0, j[1]-y+1);
        ord[px-x].push_back(d);
        px = j[0]+1;
        ord[px-x].push_back(-d);
      }
    }
    ord[max(0, (*--s.end())[0]-x+1)].push_back(0);
  }
  int ans = 1e7;
  multiset<int> act; act.insert(0);
  for(int i = 0; i < ord.size(); i ++){
    for(auto&j:ord[i]){
      if(j >= 0) act.insert(j);
      else act.erase(act.find(-j));
    }
    ans = min(ans, i+*act.rbegin());
  }
  cout << ans;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[1000005];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=1;
	pos[1]=b[1].first,h[1]=b[1].second;
	for(int i=2;i<=m;i++)
	{
		while(b[i].second>h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[top+1]=oo,h[top+1]=-1;

//	for(int i=1;i<=top;i++)
//		printf("i=%d pos=%d h=%d\n",i,pos[i],h[i]);

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		int l=1,r=top+1,mid,ans;
		while(l<=r)
		{
			mid=(l+r)>>1;
			if(pos[mid]>=x)
				r=mid-1,ans=mid;
			else
				l=mid+1;
		}

		modify(i,max(0,h[ans]+1-y));
		for(int j=ans;j<=top && h[j]>=y;j++)
		{
			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
//			printf("dx=%d dy=%d\n",dx,dy);
			v[dx].push_back(pii(i,dy));
		}
	}

	int ans=t[1];
	for(int i=1;i<=1000000;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-1,pos[top+1]=oo,h[top+1]=-1;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top+1;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));
			if(j>top)
				break;

			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
			v[dx].push_back(pii(i,dy));
			if(dy==0)
				break;
		}
	}

	int ans=t[1];
	for(int i=1;i<M;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-1,pos[top+1]=oo,h[top+1]=-1;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));

			int dx=pos[j]+1-x,dy=h[j+1]+1-y;
			v[dx].push_back(pii(i,dy));
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=2;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[top+1]=oo,h[top+1]=-1;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));

			int dx=pos[j]+1-x,dy=h[j+1]+1-y;
			v[dx].push_back(pii(i,dy));
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-1,pos[top+1]=oo,h[top+1]=-1;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));

			int dx=pos[j]+1-x,dy=h[j+1]+1-y;
			v[dx].push_back(pii(i,max(0,dy)));
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-oo,pos[top+1]=oo,h[top+1]=-oo;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top+1;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));
			if(j>top)
				break;

			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
			v[dx].push_back(pii(i,dy));
			if(dy==0)
				break;
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-oo,pos[top+1]=oo,h[top+1]=-oo;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));

			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
			v[dx].push_back(pii(i,dy));
			if(dy==0)
				break;
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[1000005];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=1;
	pos[1]=b[1].first,h[1]=b[1].second;
	for(int i=2;i<=m;i++)
	{
		while(b[i].second>h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[top+1]=oo,h[top+1]=-1;

//	for(int i=1;i<=top;i++)
//		printf("i=%d pos=%d h=%d\n",i,pos[i],h[i]);

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		int l=1,r=top+1,mid,ans;
		while(l<=r)
		{
			mid=(l+r)>>1;
			if(pos[mid]>=x)
				r=mid-1,ans=mid;
			else
				l=mid+1;
		}

		modify(i,max(0,h[ans]+1-y));
		for(int j=ans;j<=top && h[j]>=y;j++)
		{
			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
//			printf("dx=%d dy=%d\n",dx,dy);
			v[dx].push_back(pii(i,dy));
		}
	}

	int ans=t[1];
	for(int i=1;i<=1000001;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;
const int N=2005;
const int M=1000005;
const int oo=1<<30;

int n,m;
pii a[N],b[N];

int pos[N],h[N];
vector<pii> v[M];

int sz=1,t[N<<2];

inline void modify(int k,int x)
{
	k=k+sz-1;
	t[k]=x;
	k>>=1;

	while(k)
	{
		t[k]=max(t[k<<1],t[k<<1|1]);
		k>>=1;
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].first,&a[i].second);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&b[i].first,&b[i].second);

	sort(a+1,a+n+1);
	sort(b+1,b+m+1);

	while(sz<n)
		sz<<=1;

	int top=0;
	for(int i=1;i<=m;i++)
	{
		while(top && b[i].second>=h[top])
			top--;

		top++;
		pos[top]=b[i].first;
		h[top]=b[i].second;
	}
	pos[0]=-oo,pos[top+1]=oo,h[top+1]=-oo;

	for(int i=1;i<=n;i++)
	{
		int x=a[i].first,y=a[i].second;
		for(int j=1;j<=top;j++)
		{
			if(pos[j]<x)
				continue;
			if(pos[j-1]<x && pos[j]>=x)
				modify(i,max(0,h[j]+1-y));

			int dx=pos[j]+1-x,dy=max(0,h[j+1]+1-y);
			if(dy<=0)
				break;
			v[dx].push_back(pii(i,dy));
		}
	}

	int ans=t[1];
	for(int i=1;i<N;i++)
	{
		for(int j=0;j<v[i].size();j++)
			modify(v[i][j].first,v[i][j].second);
		ans=min(ans,i+t[1]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int> > rob,lig;
  for(int i = 0;i < n;i++){
    int a,b;cin>>a>>b;
    rob.push_back({a,b});
  }
  for(int i = 0;i < m;i++){
    int a,b;cin>>a>>b;
    lig.push_back({a,b});
  }
  vector<int> ri,up;
  int ans = INT_MAX,tr = 0,tl = 0;
  for(int i =0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first && rob[i].second <= lig[j].second){
        ri.push_back(lig[j].first-rob[i].first+1);
        up.push_back(1-rob[i].second+lig[j].second);
      }
    }
  }
  for(int i = 0;i < ri.size();i++){
    if(ri[i] < up[i])tr = max(tr,ri[i]);
    else tl = max(tl,up[i]);
  }
  int ter = 0,tel = 0;
  for(int i= 0;i < ri.size();i++){
    if(up[i] <= tl)continue;
    else ter = max(ter,ri[i]);
  }
  ans = tl+ter;
  for(int i =0 ;i < ri.size();i++){
    if(ri[i] <= tr)continue;
    else tel = max(tel,up[i]);
  }
  ans = min(ans,tel+tr);
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<int> a(n),b(n),c(m),d(m);
  for(int i =0 ;i < n;i++)cin>>a[i]>>b[i];
  for(int i = 0;i < m;i++)cin>>c[i]>>d[i];
  vector<int> dp(1000010,0);
  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      if(a[i] <= c[i])dp[c[i]-a[i]] = max(dp[c[i]-a[i]],d[i]-b[i]+1LL);
    }
  }
  int ans = INT_MAX,mx = 0;
  for(int i = 1000000;i >= 0;i--){
    mx = max(mx,dp[i]);
    ans = min(ans,mx+i);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int> > rob,lig;
  for(int i = 0;i < n;i++){
    int a,b;cin>>a>>b;
    rob.push_back({a,b});
  }
  for(int i = 0;i < m;i++){
    int a,b;cin>>a>>b;
    lig.push_back({a,b});
  }
  vector<int> ri,up;
  int ans = INT_MAX,tr = 0,tl = 0;
  for(int i =0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first && rob[i].second <= lig[j].second){
        ri.push_back(lig[j].first-rob[i].first+1);
        up.push_back(1-rob[i].second+lig[j].second);
      }
    }
  }
  for(int i = 0;i < ri.size();i++){
    int r = ri[i],tempu = 0;
    for(int j = 0;j < up.size();j++){
      if(ri[j] <= r)continue;
      tempu = max(tempu,up[j]);
    }
    ans = min(ans,r+tempu);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int> > rob,lig;
  for(int i = 0;i < n;i++){
    int a,b;cin>>a>>b;
    rob.push_back({a,b});
  }
  for(int i = 0;i < m;i++){
    int a,b;cin>>a>>b;
    lig.push_back({a,b});
  }
  vector<int> ri,up;
  int ans = INT_MAX,tr = 0,tl = 0;
  for(int i =0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first && rob[i].second <= lig[j].second){
        ri.push_back(lig[j].first-rob[i].first+1);
        up.push_back(1-rob[i].second+lig[j].second);
      }
    }
  }
  for(int i = 0;i < ri.size();i++){
    int r = ri[i],tempu = 0;
    for(int j = 0;j < up.size();j++){
      if(ri[j] <= r)continue;
      tempu = max(tempu,up[j]);
    }
    ans = min(ans,r+tempu);
  }
  for(int i = 0;i < up.size();i++){
    int u = up[i],tempr = 0;
    for(int j = 0;j < ri.size();j++){
      if(up[j] <= u)continue;
      tempr = max(tempr,ri[j]);
    }
    ans = min(ans,u+tempr);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<int> a(n),b(n),c(m),d(m);
  for(int i =0 ;i < n;i++)cin>>a[i]>>b[i];
  for(int i = 0;i < m;i++)cin>>c[i]>>d[i];
  vector<int> dp(1000010,0);
  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      if(a[i] <= c[j])dp[c[j]-a[i]] = max(dp[c[j]-a[i]],d[j]-b[i]+1LL);
    }
  }
  int ans = INT_MAX,mx = 0;
  for(int i = 1000000;i >= 0;i--){
    mx = max(mx,dp[i]);
    ans = min(ans,mx+i);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  pair<int,int> rob[n],lig[n];
  for(int i =0 ;i < n;i++)cin>>rob[i].first>>rob[i].second;
  for(int i = 0;i < m;i++)cin>>lig[i].first>>lig[i].second;
  int* dp = new int[1000010]{0};
  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first)dp[lig[j].first-rob[i].first] = max(dp[lig[j].first-rob[i].first],lig[j].second-rob[i].second+1LL);
    }
  }
  int ans = INT_MAX,mx = 0;
  for(int i = 1000000;i >= 0;i--){
    mx = max(mx,dp[i]);
    ans = min(ans,mx+i);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  pair<int,int> rob[n],lig[n];
  for(int i =0 ;i < n;i++)cin>>rob[i].first>>rob[i].second;
  for(int i = 0;i < m;i++)cin>>lig[i].first>>lig[i].second;
  vector<int> dp(1000010,0);
  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first)dp[lig[j].first-rob[i].first] = max(dp[lig[j].first-rob[i].first],lig[j].second-rob[i].second+1LL);
    }
  }
  int ans = INT_MAX,mx = 0;
  for(int i = 1000000;i >= 0;i--){
    mx = max(mx,dp[i]);
    ans = min(ans,mx+i);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<int> a(n),b(n),c(n),d(n);
  for(int i =0 ;i < n;i++)cin>>a[i]>>b[i];
  for(int i = 0;i < m;i++)cin>>c[i]>>d[i];
  vector<int> dp(1000010,0);
  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      if(a[i] <= c[i])dp[c[i]-a[i]] = max(dp[c[i]-a[i]],d[i]-b[i]+1LL);
    }
  }
  int ans = INT_MAX,mx = 0;
  for(int i = 1000000;i >= 0;i--){
    mx = max(mx,dp[i]);
    ans = min(ans,mx+i);
  }
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int> > rob,lig;
  for(int i = 0;i < n;i++){
    int a,b;cin>>a>>b;
    rob.push_back({a,b});
  }
  for(int i = 0;i < m;i++){
    int a,b;cin>>a>>b;
    lig.push_back({a,b});
  }
  vector<int> ri,up;
  int ans = INT_MAX,tr = 0,tl = 0;
  for(int i =0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first && rob[i].second <= lig[j].second){
        ri.push_back(lig[j].first-rob[i].first+1);
        up.push_back(1-rob[i].second+lig[j].second);
      }
    }
  }
  for(int i = 0;i < ri.size();i++){
    int r = ri[i],tempu = 0;
    for(int j = 0;j < up.size();j++){
      if(ri[j] <= r)continue;
      tempu = max(tempu,up[j]);
    }
    ans = min(ans,r+tempu);
  }
  for(int i = 0;i < up.size();i++){
    int u = up[i],tempr = 0;
    for(int j = 0;j < ri.size();j++){
      if(up[j] <= u)continue;
      tempr = max(tempr,ri[j]);
    }
    ans = min(ans,u+tempr);
  }
  for(int i = 0;i < ri.size();i++){
    if(ri[i] < up[i])tr = max(tr,ri[i]);
    else tl = max(tl,up[i]);
  }
  int ter = 0,tel = 0;
  for(int i= 0;i < ri.size();i++){
    if(up[i] <= tl)continue;
    else ter = max(ter,ri[i]);
  }
  ans = min(ans,tl+ter);
  for(int i =0 ;i < ri.size();i++){
    if(ri[i] <= tr)continue;
    else tel = max(tel,up[i]);
  }
  ans = min(ans,tel+tr);
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
#define endl "\n"
#define pi acos(-1)
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
void solve(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int> > rob,lig;
  for(int i = 0;i < n;i++){
    int a,b;cin>>a>>b;
    rob.push_back({a,b});
  }
  for(int i = 0;i < m;i++){
    int a,b;cin>>a>>b;
    lig.push_back({a,b});
  }
  vector<int> ri,up;
  int ans = INT_MAX,tr = 0,tl = 0;
  for(int i =0 ;i < n;i++){
    for(int j = 0;j < m;j++){
      if(rob[i].first <= lig[j].first && rob[i].second <= lig[j].second){
        ri.push_back(lig[j].first-rob[i].first+1);
        up.push_back(1-rob[i].second+lig[j].second);
      }
    }
  }
  for(int i = 0;i < ri.size();i++){
    int r = ri[i],tempu = 0;
    for(int j = 0;j < up.size();j++){
      if(ri[j] <= r)continue;
      tempu = max(tempu,up[j]);
    }
    ans = min(ans,r+tempu);
  }
  for(int i = 0;i < up.size();i++){
    int u = up[i],tempr = 0;
    for(int j = 0;j < ri.size();j++){
      if(up[j] <= u)continue;
      tempr = max(tempr,ri[j]);
    }
    ans = min(ans,u+tempr);
  }
  for(int i = 0;i < ri.size();i++){
    if(ri[i] < up[i])tr = max(tr,ri[i]);
    else tl = max(tl,up[i]);
  }
  int ter = 0,tel = 0;
  for(int i= 0;i < ri.size();i++){
    if(up[i] <= tl)continue;
    else ter = max(ter,ri[i]);
  }
  ans = tl+ter;
  for(int i =0 ;i < ri.size();i++){
    if(ri[i] <= tr)continue;
    else tel = max(tel,up[i]);
  }
  ans = min(ans,tel+tr);
  cout<<ans<<endl;
}
int32_t main()
{
    fastttt
    int t = 1;
    //cin>>t;
    for(int j = 1;j <= t;j++)
    {
		solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;

    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = v[i].se ;
        if(i!=(v.size()-1))
            dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({max(0LL,cd[j].fi-ab[i].fi),max(0LL,cd[j].se-ab[i].se)}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi(v.size()+5,0) ;
    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            if(ab[i].fi<=cd[j].fi&&ab[i].se<=cd[j].se)
                v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;

    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = v[i].se ;

        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;
    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;
    if(v[v.size()-1].fi<0||v[v.size()-1].se<0)
    {
        cout << 0 << "\n" ;
        return ;
    }
    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;
    dmaxi[v.size()-1] = v[v.size()-1].se ;
    for(int i=v.size()-2 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+max(dmaxi[t]+1,0LL)) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;
    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({max(0LL,cd[j].fi-ab[i].fi),max(0LL,cd[j].se-ab[i].se)}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi(v.size()+5,0) ;
    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000000 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            if(ab[i].fi<=cd[j].fi&&ab[i].se<=cd[j].se)
                v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    if(v.size()==0)
    {
        cout << 0 << "\n" ;
        return ;
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;

    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = v[i].se ;

        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;

    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mod 1000000007

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

int n,m ;
int arr[500005] ;
string str ;
vector<pii> ab ;
vector<pii> cd ;

void solve()
{
    cin >> n >> m ;

    for(int i=0 ; i<n ; i++)
    {
        int a,b ;
        cin >> a >> b ;
        ab.pb({a,b}) ;
    }

    for(int i=0 ; i<m ; i++)
    {
        int c,d ;
        cin >> c >> d ;
        cd.pb({c,d}) ;
    }
    sort(cd.begin(),cd.end()) ;

    vector<pii> v ;
    for(int i=0 ; i<n ; i++)
    {
        for(int j=0 ; j<m ; j++)
        {
            v.pb({cd[j].fi-ab[i].fi,cd[j].se-ab[i].se}) ;
        }
    }
    sort(v.begin(),v.end()) ;
    vector<int> dmaxi((int)(v.size())+5LL,0LL) ;
    for(int i=v.size()-1 ; i>=0 ; i--)
    {
        dmaxi[i] = max(dmaxi[i+1],v[i].se) ;
    }
    int ans = 5000000 ;
    if(v[v.size()-1].fi<=0&&v[v.size()-1].se<=0)
    {
        cout << 0 << "\n" ;
        return ;
    }
    for(int a=0 ; a<=1000002 ; a++)
    {
        if(a>v[v.size()-1].fi)
        {
            ans = min(ans,a) ;
            continue ;
        }
        int low = 0 ;
        int high = v.size()-1 ;
        int t = 0 ;
        while(low<=high)
        {
            int mid = (low+high)/2 ;
            if(v[mid].fi>=a)
            {
                t = mid ;
                high = mid-1 ;
            }
            else
            {
                low = mid+1 ;
            }
        }
        ans = min(ans,a+dmaxi[t]+1) ;
    }
    cout << ans << "\n" ;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	//cin >> tt ;
	while(tt--)
	{
	     solve() ;
	}
}


////////////////////      We are not beautiful     /////////////////////////////////

//Coded by Aman Bharti (ECE 2k18) Nit Patna

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

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

  // assert(freopen("soln.in","r",stdin));
  // assert(freopen("soln.out","w",stdout));

  ll n,m;
  cin>>n>>m;
  vector<pair<ll,ll>>rob;
  vector<pair<ll,ll>>sl;
  ll maxm[4000005];
  memset(maxm,-1,sizeof(maxm));

  for(int i=0;i<n;i++){
    ll x,y;
    cin>>x>>y;
    rob.push_back({x,y});
  }

  for(int i=0;i<m;i++){
    ll x,y;
    cin>>x>>y;
    sl.push_back({x,y});
  }

  ll ans=INT64_MAX;
  ll mxx=0,mxy=0;

  for(int i=0;i<m;i++)
  for(int j=0;j<n;j++)
  if(rob[j].first<=sl[i].first&&rob[j].second<=sl[i].second)
  maxm[sl[i].first-rob[j].first] = max((maxm[sl[i].first-rob[j].first]),(sl[i].second-rob[j].second));

  for(ll i= 4000002;i>=0;i--){
    ans =min(i+mxy+1,ans);
    mxy = max(maxm[i]+1,mxy);
  }
  ans =min(mxy,ans);

  cout<<ans<<endl;
  return 0;
}


////////////////////      We are not beautiful     /////////////////////////////////

//Coded by Aman Bharti (ECE 2k18) Nit Patna

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

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

  // assert(freopen("soln.in","r",stdin));
  // assert(freopen("soln.out","w",stdout));

  int n,m;
  cin>>n>>m;
  vector<pair<int,int>>rob;
  vector<pair<int,int>>sl;
  int maxm[4000005];
  memset(maxm,-1,sizeof(maxm));

  for(int i=0;i<n;i++){
    int x,y;
    cin>>x>>y;
    rob.push_back({x,y});
  }

  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    sl.push_back({x,y});
  }

  int ans=INT_MAX;
  int mxx=0,mxy=0;

  for(int i=0;i<m;i++)
  for(int j=0;j<n;j++)
  if(rob[j].first<=sl[i].first&&rob[j].second<=sl[i].second){
    // cout<<"okk"<<endl;
    maxm[abs(sl[i].first-rob[j].first)] = max(maxm[abs(sl[i].first-rob[j].first)],sl[i].second-rob[j].second);
  }

  for(int i= 300001;i>=0;i--){
    //cout<<i+mxy+1<<" "<<maxm[i]<<" "<<i<<endl;
    ans =min(i+mxy+1,ans);
    mxy = max(maxm[i]+1,mxy);
  }
  ans =min(mxy,ans);
  cout<<ans<<endl;
  return 0;
}


    //23 days left
    //CODEFORCES:-
    //coded by AMAN BHARTI NIT PATNA
    //ECE 2K18

    //After reading solution of kasun]
    //so complicated

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

    #define home
    #define TASK "soln"
    #define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
    #define pb push_back
    #define pp pop_back
    #define ll long long int
    #define ull unsigned long long
    #define br cout<<endl
    #define fo for(int i=0;i<n;i++)
    #define foo for(int i=1;i<=n;i++)
    #define rep(i,a,n) for(int i=a;i<n;i++)
    #define repi(i,a,n) for(int i=a;i>=n;i--)
    #define mod 1000000007
    ll gcd(ll a,ll b);
    ll power(ll a,ll b);
    ll powerwithmod(ll a,ll b);
    ll nCr(ll n,ll r);
    ll fact(ll n);

    void solve(){
        ll n,m;
        cin>>n>>m;
        vector<pair<ll,ll>>rob(n),lig(m);
        for(int i=0;i<n;i++){cin>>rob[i].first>>rob[i].second;}
        for(int i=0;i<m;i++){cin>>lig[i].first>>lig[i].second;}

        vector<ll>maxY_atx(1e6+8,0);

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(rob[j].first<=lig[i].first&&rob[j].second<=lig[i].second){
                    ll xdiff = lig[i].first-rob[j].first;
                    ll ydiff = lig[i].second-rob[j].second;
                    maxY_atx[xdiff]=max(maxY_atx[xdiff],ydiff);
                }
                else continue;
            }
        }

        // for(int i=0;i<=10;i++)cout<<maxY_atx[i]<<" ";
        // br;

        ll sol =INT64_MAX;
        ll ymax = 0;
        for(int i=1e6+7;i>=0;i--){
            ymax = max(ymax,maxY_atx[i]);
            sol = min(sol,ymax+i);
        }

        //cout<<ymax<<" ";
        cout<<sol;
        br;
    }

    int main(){
         #ifdef home
            assert(freopen(TASK".in","r",stdin));
            assert(freopen(TASK".out","w",stdout));
        #endif

        ios::sync_with_stdio(false);
        cin.tie(NULL);

        // int t;
        // cin>>t;
        // while(t-->0)
        solve();

        #ifdef home
            eprintf("Time: %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
        #endif

        return 0;
    }

    ll gcd(ll a,ll b) {if(b==0)return a;return gcd(b,a%b);}
    ll powerwithmod(ll a,ll b) {ll res=1;while(b>0){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res%mod;}
    ll power(ll a,ll b) {ll res=1;while(b>0){if(b&1)res=(res*a);a=(a*a);b>>=1;}return res;}
    ll nCr(ll n, ll r){return fact(n) / (fact(r) * fact(n - r));}
    ll fact(ll n){ll res = 1;for(ll i = 2; i <= n; i++)res = (res * i)%mod;return (res)%mod;}


    //23 days left
    //CODEFORCES:-
    //coded by AMAN BHARTI NIT PATNA
    //ECE 2K18

    //After reading solution of kasun]
    //so complicated

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

    //#define home
    #define TASK "soln"
    #define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
    #define pb push_back
    #define pp pop_back
    #define ll long long int
    #define ull unsigned long long
    #define br cout<<endl
    #define fo for(int i=0;i<n;i++)
    #define foo for(int i=1;i<=n;i++)
    #define rep(i,a,n) for(int i=a;i<n;i++)
    #define repi(i,a,n) for(int i=a;i>=n;i--)
    #define mod 1000000007
    ll gcd(ll a,ll b);
    ll power(ll a,ll b);
    ll powerwithmod(ll a,ll b);
    ll nCr(ll n,ll r);
    ll fact(ll n);

    void solve(){
        ll n,m;
        cin>>n>>m;
        vector<pair<ll,ll>>rob(n),lig(m);
        for(int i=0;i<n;i++){cin>>rob[i].first>>rob[i].second;}
        for(int i=0;i<m;i++){cin>>lig[i].first>>lig[i].second;}

        vector<ll>maxY_atx(1e6+8,0);

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(rob[j].first<=lig[i].first&&rob[j].second<=lig[i].second){
                    ll xdiff = lig[i].first-rob[j].first;
                    ll ydiff = lig[i].second-rob[j].second;
                    maxY_atx[xdiff]=max(maxY_atx[xdiff],ydiff);
                }
                else continue;
            }
        }

        // for(int i=0;i<=10;i++)cout<<maxY_atx[i]<<" ";
        // br;

        ll sol =INT64_MAX;
        ll ymax = 0;
        for(int i=1e6+7;i>=0;i--){
            ymax = max(ymax,maxY_atx[i]);
            sol = min(sol,ymax+i);
        }

        //cout<<ymax<<" ";
        cout<<sol;
        br;
    }

    int main(){
         #ifdef home
            assert(freopen(TASK".in","r",stdin));
            assert(freopen(TASK".out","w",stdout));
        #endif

        ios::sync_with_stdio(false);
        cin.tie(NULL);

        // int t;
        // cin>>t;
        // while(t-->0)
        solve();

        #ifdef home
            eprintf("Time: %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
        #endif

        return 0;
    }

    ll gcd(ll a,ll b) {if(b==0)return a;return gcd(b,a%b);}
    ll powerwithmod(ll a,ll b) {ll res=1;while(b>0){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res%mod;}
    ll power(ll a,ll b) {ll res=1;while(b>0){if(b&1)res=(res*a);a=(a*a);b>>=1;}return res;}
    ll nCr(ll n, ll r){return fact(n) / (fact(r) * fact(n - r));}
    ll fact(ll n){ll res = 1;for(ll i = 2; i <= n; i++)res = (res * i)%mod;return (res)%mod;}


////////////////////      We are not beautiful     /////////////////////////////////

//Coded by Aman Bharti (ECE 2k18) Nit Patna

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

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

  // assert(freopen("soln.in","r",stdin));
  // assert(freopen("soln.out","w",stdout));

  int n,m;
  cin>>n>>m;
  vector<pair<int,int>>rob;
  vector<pair<int,int>>sl;
  int maxm[300005];
  memset(maxm,-1,sizeof(maxm));

  for(int i=0;i<n;i++){
    int x,y;
    cin>>x>>y;
    rob.push_back({x,y});
  }

  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    sl.push_back({x,y});
  }

//  sort(rob.begin(),rob.end());
//  sort(sl.begin(),sl.end(),greater<>());

  int ans=INT_MAX;
  int mxx=0,mxy=0;

  for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
      if(rob[j].first<=sl[i].first&&rob[j].second<=sl[i].second){
        //cout<<sl[j].first<<" "<<sl[j].second<<" "<<rob[i].first<<" "<<rob[i].second<<" "<<sl[i].second-rob[j].second<<" "<<sl[i].first-rob[j].first<<endl;
        maxm[sl[i].first-rob[j].first] = max(maxm[sl[i].first-rob[j].first],sl[i].second-rob[j].second);
      }
    }
  }

  for(int i= 300002;i>=0;i--){
    //cout<<maxm[i]<<" "<<mxy<<endl;
    ans =min(i+mxy+1,ans);
    mxy = max(maxm[i]+1,mxy);
  }

  cout<<ans<<endl;
  return 0;
}


////////////////////      We are not beautiful     /////////////////////////////////

//Coded by Aman Bharti (ECE 2k18) Nit Patna

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

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

  // assert(freopen("soln.in","r",stdin));
  // assert(freopen("soln.out","w",stdout));

  int n,m;
  cin>>n>>m;
  vector<pair<int,int>>rob;
  vector<pair<int,int>>sl;
  int maxm[300005];
  memset(maxm,-1,sizeof(maxm));

  for(int i=0;i<n;i++){
    int x,y;
    cin>>x>>y;
    rob.push_back({x,y});
  }

  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    sl.push_back({x,y});
  }

  int ans=INT_MAX;
  int mxx=0,mxy=0;

  for(int i=0;i<m;i++)
  for(int j=0;j<n;j++)
  if(rob[j].first<=sl[i].first&&rob[j].second<=sl[i].second){
    // cout<<"okk"<<endl;
    maxm[sl[i].first-rob[j].first] = max(maxm[sl[i].first-rob[j].first],sl[i].second-rob[j].second);
  }

  for(int i= 300001;i>=0;i--){
    //cout<<i+mxy+1<<" "<<maxm[i]<<" "<<i<<endl;
    ans =min(i+mxy+1,ans);
    mxy = max(maxm[i]+1,mxy);
  }
  ans =min(mxy,ans);
  cout<<ans<<endl;
  return 0;
}


////////////////////      We are not beautiful     /////////////////////////////////

//Coded by Aman Bharti (ECE 2k18) Nit Patna

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

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

  // assert(freopen("soln.in","r",stdin));
  // assert(freopen("soln.out","w",stdout));

  int n,m;
  cin>>n>>m;
  vector<pair<int,int>>rob;
  vector<pair<int,int>>sl;
  int maxm[300005];
  memset(maxm,-1,sizeof(maxm));

  for(int i=0;i<n;i++){
    int x,y;
    cin>>x>>y;
    rob.push_back({x,y});
  }

  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    sl.push_back({x,y});
  }

//  sort(rob.begin(),rob.end());
//  sort(sl.begin(),sl.end(),greater<>());

  int ans=INT_MAX;
  int mxx=0,mxy=0;

  for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
      if(rob[j].first<=sl[i].first&&rob[j].second<=sl[i].second){
        maxm[sl[i].first-rob[j].second] = max(maxm[j],sl[i].second-rob[j].second);
      }
    }
  }

  for(int i= 300001;i>=0;i--){
    ans =min(maxm[i]+mxy+1,ans);
    mxy = max(maxm[i]+1,mxy);
  }

  cout<<ans<<endl;
  return 0;
}