#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;

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];

    bool biosw=false;
    if (sb<sd) { biosw=true; 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;
        }
    }

    ress=cnt.size();
    cout<<ress<<endl;
    for (auto it:cnt)
    {
        cout<<(it.first).xx<<" "<<(it.first).second<<" "<<it.second<<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];


    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
*/