#include <bits/stdc++.h>
#define ll 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];
    ll t=1;
    for (ll i=1;i<=n;i++)
    {
        while (true)
        {
            if (!a[i] || t>m)
                break;
            if (!d[t])
            {
                while (!d[t] && t<=m)
                    t++;
                continue;
            }
            ll minn=min(a[i],d[t]);
            if (!minn)
                continue;
            sum+=minn;
            a[i]-=minn;
            b[i]-=minn;
            if (minn>c[t])
                c[t]=0;
            else
                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 (!b[t])
            {
                while (!b[t] && t<=n)
                    t++;
                continue;
            }
            ll minn=min(c[i],b[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;
}