#include <bits/stdc++.h>
#define ll unsigned long long
#define MAXN 50010
using namespace std;
ll a[MAXN],b[MAXN],c[MAXN],d[MAXN];
set<pair<ll,ll> > s;
map<pair<ll,ll>,ll> val;
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,m,sum=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];
    /*vector<pair<ll,ll> > red,kol;
    for (ll i=1;i<=n;i++)
        red.push_back({a[i],i});
    for (ll i=1;i<=m;i++)
        kol.push_back({c[i],i});
    sort(red.begin(),red.end());
    sort(kol.begin(),kol.end());
    ll ri=0,ki=0;
    while (ri<n && ki<m)
    {
        if (a[red[ri].second]==0)
        {
            ri++;
            continue;
        }
        if (c[kol[ki].second]==0)
        {
            ri++;
            continue;
        }
        ll minn=min(red[ri].first,kol[ki.second]);
        a[red[ri].second]-=minn;
        b[red[ri].second]-=minn;
        c[kol[ki].second]-=minn;
        d[kol[ki].second]-=minn;
    }*/
    ll t=1;
    for (ll i=1;i<=n;i++)
    {
        while (true)
        {
            if (!a[i] || t>m)
                break;
            if (!c[t])
            {
                while (!c[t] && t<=m)
                    t++;
                continue;
            }
            ll minn=min(a[i],c[t]);
            if (!minn)
                continue;
            sum+=minn;
            a[i]-=minn;
            b[i]-=minn;
            c[t]-=minn;
            d[t]-=minn;
            s.insert({i,t});
            val[{i,t}]+=minn;
        }
    }
    t=1;
    for (ll i=1;i<=m;i++)
    {
        while (true)
        {
            if (!c[i] || t>n)
                break;
            if (!a[t])
            {
                while (!a[t] && t<=n)
                    t++;
                continue;
            }
            ll minn=min(c[i],a[t]);
            if (!minn)
                continue;
            sum+=minn;
            c[i]-=minn;
            d[i]-=minn;
            b[t]-=minn;
            s.insert({t,i});
            val[{t,i}]+=minn;
        }
    }
    t=1;
    for (ll i=1;i<=n;i++)
    {
        while (true)
        {
            if (!b[i] || t>m)
                break;
            if (!d[t])
            {
                while (!d[t] && t<=m)
                    t++;
                continue;
            }
            ll minn=min(b[i],d[t]);
            if (!minn)
                continue;
            sum+=minn;
            b[i]-=minn;
            d[t]-=minn;
            s.insert({i,t});
            val[{i,t}]+=minn;
        }
    }
    t=1;
    for (ll i=1;i<=m;i++)
    {
        while (true)
        {
            if (!d[i] || t>n)
                break;
            if (!b[t])
            {
                while (!b[t] && t<=n)
                    t++;
                continue;
            }
            ll minn=min(d[i],b[t]);
            if (!minn)
                continue;
            sum+=minn;
            d[i]-=minn;
            b[i]-=minn;
            s.insert({t,i});
            val[{t,i}]+=minn;
        }
    }
    cout << sum << "\n" << s.size() << "\n";
    for (auto it : s)
    {
        cout << it.first << " " << it.second << " " << val[it] << "\n";
    }
    return 0;
}