#include <bits/stdc++.h>
#define here cerr<<"==========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#define ll long long
#define endl '\n'
#define sz(a) (ll)(a.size())
#define fi first
#define sc second
#define pll pair<ll,ll>
#define llinf 1000000000LL
#define pb push_back
using namespace std;
#define maxn 50005
ll n,m;
map<pll,ll> cnt;
struct nod{
    ll l,r,id,val;
};
bool cmp(nod a,nod b){return a.l<b.l;}
nod a[maxn], b[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].l >> a[i].r;
    for(ll i = 1;i<=m;i++) cin >> b[i].l >> b[i].r;
    for(ll i = 1;i<=n;i++) a[i].val = 0,a[i].id = i;
    for(ll i = 1;i<=m;i++) b[i].val = 0,b[i].id = i;
    ll al = 0,bl = 0;
    for(ll i = 1;i<=n;i++) al+=a[i].l;
    for(ll i = 1;i<=m;i++) bl+=b[i].l;
    bool swp = 0;
    if(al<bl){
        for(ll i = 1;i<=max(n,m);i++) swap(a[i],b[i]);
        swap(n,m);
        swp = 1;
    }
    sort(a+1,a+1+n,cmp);
    sort(b+1,b+1+m,cmp);
    ll j = m;
    for(ll i = n;i>=1;i--){
        while(a[i].val<a[i].l&&j>=1){
            ll x = min(b[j].l-b[j].val,a[i].l-a[i].val);
            if(b[j].l==b[j].val){j--; continue;}
            a[i].val+=x;
            b[j].val+=x;
            cnt[{a[i].id,b[j].id}]+=x;
            j--;
        }
    }
    j = m;
    for(ll i = n;i>=1;i--){
        while(a[i].val<a[i].l&&j>=1){
            ll x = min(b[j].r-b[j].val,a[i].l-a[i].val);
            if(b[j].r==b[j].val){j--; continue;}
            a[i].val+=x;
            b[j].val+=x;
            cnt[{a[i].id,b[j].id}]+=x;
            j--;
        }
    }
    j = m;
    for(ll i = n;i>=1;i--){
        while(a[i].val<a[i].r&&j>=1){
            ll x = min(b[j].r-b[j].val,a[i].r-a[i].val);
            if(b[j].val==b[j].r){j--; continue;}
            a[i].val+=x;
            b[j].val+=x;
            cnt[{a[i].id,b[j].id}]+=x;
            j--;
        }
    }
    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;
        if(swp) swap(p.fi,p.sc);
        cout<<p.fi<<" "<<p.sc<< " "<<it.sc<<endl;
    }
    return 0;
}
/**
2 3
2 2
2 2
1 2
1 2
1 2

3 2
1 2
1 2
1 2
2 2
2 2

2 2
1 2
1 1
1 3
0 0

2 2
1 3
0 0
1 2
1 1
**/