#include <bits/stdc++.h>
#define xx first
#define yy second

typedef long long ll;

using namespace std;

const ll maxn=50010;

pair<ll,ll> a[maxn];
pair<ll,ll> b[maxn];

ll ta[maxn];
ll tb[maxn];

int main(){
    ll n,m;
    cin>>n>>m;
    ll suma=0;
    ll sumb=0;
    for(ll i=0;i<n;i++) {cin>>a[i].xx>>a[i].yy; suma+=a[i].yy;}
    for(ll i=0;i<m;i++) {cin>>b[i].xx>>b[i].yy; sumb+=b[i].yy;}
    bool okr=false;
    if(suma>sumb){
        okr=true;
        swap(suma,sumb);
        swap(a,b);
        swap(n,m);
    }
    ll pa=0;
    vector<tuple<ll,ll,ll> > res;
    for(ll pb=0;pb<m;pb++){
        while(tb[pb]<b[pb].xx){
            ll t=min(b[pb].xx-tb[pb],a[pa].yy-ta[pa]);
            res.push_back({pa,pb,t});
            tb[pb]+=t;
            ta[pa]+=t;
            if(a[pa].yy==ta[pa]) pa++;
        }
    }
    ll pb=0;
    for(pa=0;pa<n;pa++){
        while(ta[pa]<a[pa].yy){
            ll t=min(a[pa].yy-ta[pa],b[pb].yy-tb[pb]);
            res.push_back({pa,pb,t});
            ta[pa]+=t;
            tb[pb]+=t;
            if(b[pb].yy==tb[pb]) pb++;
        }
    }
    sort(res.begin(),res.end());
    vector<tuple<ll,ll,ll> > pres;
    tuple<ll,ll,ll> prosli={-1,-1,-1};
    for(auto i:res){
        if(get<0>(i)==get<0>(prosli) && get<1>(i)==get<1>(prosli)){
            get<2>(prosli)+=get<2>(i);
        }else{
            pres.push_back(prosli);
            prosli=i;
        }
    }
    pres.push_back(prosli);
    cout<<suma<<"\n";
    cout<<pres.size()-1<<"\n";
    for(auto i:pres){
        if(get<0>(i)==-1) continue;
        if(okr) swap(get<0>(i),get<1>(i));
        cout<<get<0>(i)+1<<" "<<get<1>(i)+1<<" "<<get<2>(i)<<"\n";
    }
    return 0;
}