#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define sz(a) (ll)(a.size())
#define fi first
#define sc second
#define pll pair<ll,ll>
using namespace std;
#define maxn 50005
ll n,m;
ll a[maxn],b[maxn],c[maxn],d[maxn];
map<pll,ll> cnt;
struct nod{
    ll l,r,id;
};
bool cmp(nod a,nod b){return a.l<b.l;}
nod e[maxn];
int main()
{
    ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cerr.tie(0);
    cin >> n >> m;
    for(ll i = 1;i<=n;i++) cin >> a[i] >> b[i];
    for(ll i = 1;i<=m;i++) cin >> c[i] >> d[i];
    ll sa = 0,sc = 0,sb = 0,sd = 0;
    for(ll i = 1;i<=n;i++) sa+=a[i];
    for(ll i = 1;i<=n;i++) sb+=b[i];
    for(ll i = 1;i<=m;i++) sc+=c[i];
    for(ll i = 1;i<=m;i++) sd+=d[i];
    bool ab = 1;
    for(ll i = 1;i<=n;i++) ab&=(a[i]==b[i]);
    if(sa==0&&sc==0){
        ll j = 1;
        for(ll i = 1;i<=n;i++){
            while(b[i]>d[j]&&j<=m){
                if(d[j]==0){j++; continue;}
                cnt[{i,j}] = d[j];
                b[i]-=d[j];
                j++;
            }
            if(j==m+1) break;
            if(b[i]){
                cnt[{i,j}] = b[i];
                d[j]-=b[i];
            }
        }
        cout<<min(sb,sd)<<endl;
        cout<<sz(cnt)<<endl;
        for(auto it : cnt){
            pll p = it.fi;
            ll x = p.fi;
            ll y = p.sc;
            cout<<x<< " "<<y<< " "<<it.sc<<endl;
        }
        return 0;
    }
    if(ab){
        for(ll i = 1;i<=m;i++){
            e[i].l = c[i];
            e[i].r = d[i];
            e[i].id = i;
        }
        sort(e+1,e+1+m,cmp);
        ll j = m;
        for(ll i = 1;i<=n;i++){
            while(a[i]&&j>=1){
                ll &l = e[j].l,&r = e[j].r, id = e[j].id;
                if(l==0){j--; continue;}
                ll x = min(l,a[i]);
                a[i]-=x;
                l-=x;
                cnt[{i,id}]+=x;
            }
        }
        for(ll i = 1;i<=m;i++){
            e[i].l = d[i]-c[i];
            e[i].id = i;
        }
        j = m;
        for(ll i = 1;i<=n;i++){
            while(a[i]&&j>=1){
                ll &l = e[j].l,&r = e[j].r, id = e[j].id;
                if(l==0){j--; continue;}
                ll x = min(l,a[i]);
                a[i]-=x;
                l-=x;
                cnt[{i,id}]+=x;
            }
        }
        ll ans = 0;
        for(auto it : cnt) ans+=it.sc;
        cout<<ans<<endl;
        cout<<sz(cnt)<<endl;
        for(auto it : cnt){
            pll p = it.fi;
            ll x = p.fi;
            ll y = p.sc;
            cout<<x<< " "<<y<< " "<<it.sc<<endl;
        }
    }
    return 0;
}