#include <bits/stdc++.h>

#define mod 1e9+7
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using ll = long long;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n,m,nmi=0,nma=0,mmi=0,mma=0,dif0=0;
    vector <pair<ll,ll>> vn,vm;

    cin >> n;
    cin >> m;

    for(int i=0; i<n; i++)
    {
        ll a,b;

        cin >> a;
        cin >> b;

        vn.pb(mp(a,b));

        nmi+=a;
        nma+=b;
    }
    for(int i=0; i<m; i++)
    {
        ll c,d;

        cin >> c;
        cin >> d;

        vm.pb(mp(c,d));

        if(c>0) dif0++;

        mmi+=c;
        mma+=d;
    }

    if(n==1)
    {
        if(nma>=mma)
        {
            cout << mma << "\n";
            cout << m << "\n";
            for(int i=0; i<m; i++)
            {
                cout << 1 << " " << i+1 << " " << vm[i].se << "\n";
            }
        }
        else
        {
            cout << nma << "\n";

            vector <ll> v1(m,0);

            ll cnt=m;

            for(int i=0; i<m; i++)
            {
                nma-=vm[i].fi;
                v1[i]=vm[i].fi;
                if(v1[i]!=0)cnt--;
                cout << "        " << cnt << endl;
            }
            for(int i=0; i<m && nma>0; i++)
            {
                ll k = min(nma, vm[i].se-vm[i].fi);
                nma-=k;
                if(v1[i]==0)cnt--;
                v1[i]+=k;
            }

            cout << m-cnt << "\n";
            for(int i=0; i<m; i++)
            {
                cout << 1 << " " << i+1 << " " << v1[i] << "\n";
            }
        }
    }

    return 0;
}