#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 50010

using namespace std;

int n, m;
ll a[MAXN], b[MAXN], c[MAXN], d[MAXN];
ll ress;
map<pii, ll> cnt;
ll sa, sc;
bool bs2;

void resi()
{
    ll sb=0, sd=0;
    for (int i=1; i<=n; i++) sb+=b[i];
    for (int i=1; i<=m; i++) sd+=d[i];

    //cout<<sd<<" "<<sb<<endl;

    bool biosw=bs2;
    if (sb<sd) { biosw=(!bs2); swap(n, m); swap(sb, sd); swap(b, d); }
    ///SB>=SD
    //for (int i=0; i<n; i++) bb.pb({b[i], i});
    //sort(bb.begin(), bb.end(), greater<pii>());
    int i=1;
    for (int j=1; j<=m; j++)
    {
        //cout<<i<<" "<<j<<" "<<b[i]<<" "<<d[j]<<endl;
        if (d[j]==0) continue;
        while (d[j]>=b[i])
        {
            if (b[i]==0) { i++; continue; }
            if (!biosw) cnt[{i, j}]+=b[i];
            else cnt[{j, i}]+=b[i];

            d[j]-=b[i];
            b[i]=0;
            i++;
        }
        if (d[j])
        {
            if (!biosw) cnt[{i, j}]+=d[j];
            else cnt[{j, i}]+=d[j];

            b[i]-=d[j];
            d[j]=0;
        }
    }

    for (auto it:cnt)
        ress+=(it.second);
    ll sta=cnt.size();
    cout<<ress<<endl<<sta<<endl;
    for (auto it:cnt)
    {
        cout<<(it.first).xx<<" "<<(it.first).second<<" "<<it.second<<endl;
    }
}

/*void ispisi()
{
    for (auto it:cnt)
        ress+=(it.second);
    ll sta=cnt.size();
    cout<<ress<<endl<<sta<<endl;
    for (auto it:cnt)
    {
        cout<<(it.first).xx<<" "<<(it.first).second<<" "<<it.second<<endl;
    }
    cout<<endl<<endl<<endl;
}

void ispisiniz()
{
    for (int i=1; i<=n; i++) cout<<a[i]<<" "<<b[i]<<endl;
    for (int i=1; i<=m; i++) cout<<c[i]<<" "<<d[i]<<endl;
    cout<<"kraj niza"<<endl<<endl<<endl;
}*/

int main()
{
    cin>>n>>m;

    for (int i=1; i<=n; i++) cin>>a[i]>>b[i];
    for (int i=1; i<=m; i++) cin>>c[i]>>d[i];

    for (int i=1; i<=n; i++) sa+=a[i];
    for (int i=1; i<=m; i++) sc+=c[i];

    if (sa==0 && sc==0) { resi(); exit(0); }

    //cout<<"e"<<endl;

    bool bs1=false;
    if (sa<sc) { bs1=true; swap(n, m); swap(a, c); swap(b, d); swap(sa, sc); }

    int i=1;
    for (int j=1; j<=m; j++)
    {
        if (c[j]==0) continue;
        while (c[j]>=a[i])
        {
            if (a[i]==0) { i++; continue; }
            if (!bs1) cnt[{i, j}]+=a[i];
            else cnt[{j, i}]+=a[i];

            c[j]-=a[i];
            d[j]-=a[i];
            b[i]-=a[i];
            a[i]=0;
            i++;
        }
        if (c[j])
        {
            if (!bs1) cnt[{i, j}]+=c[j];
            else cnt[{j, i}]+=c[j];

            a[i]-=c[j];
            b[i]-=c[j];
            d[j]-=c[j];
            c[j]=0;
        }
    }

    //ispisi();
    //ispisiniz();

    bs2=(!bs1); swap(n, m); swap(a, c); swap(b, d); swap(sa, sc);
    i=1;
    for (int j=1; j<=m; j++)
    {
        if (c[j]==0) continue;
        while (c[j]>=b[i])
        {
            if (b[i]==0) { i++; continue; }
            if (!bs2) cnt[{i, j}]+=b[i];
            else cnt[{j, i}]+=b[i];

            c[j]-=b[i];
            d[j]-=b[i];
            b[i]=0;
            i++;
        }
        if (c[j])
        {
            if (!bs2) cnt[{i, j}]+=c[j];
            else cnt[{j, i}]+=c[j];

            b[i]-=c[j];
            d[j]-=c[j];
            c[j]=0;
        }
    }

    //ispisi();
    //ispisiniz();
    resi();

}
/*
2 3
0 0
1 1
0 1
0 1
0 1
*/
/*
4 5
0 6
0 3
0 1
0 0
0 1
0 7
0 8
0 4
0 5
*/