#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
using namespace std;

int n,m;
vector<pii> red,kol;

void solve(int n, int m, vector<pii> red, vector<pii> kol)
{
    map<pii,int> ans;
    long long s = 0;
    for(int i=0; i < n; i++) s+=red[i].se;
    int i = 0;
    for(int j = 0; j < m; j++)
    {
        if(kol[j].first > red[i].second)
        {
            kol[j].first -= red[i].second;
            kol[j].se -= red[i].se;
            ans[mp(i,j)]+=red[i].second;
            red[i].se=0;
            ++i;
            --j;
        }
        else
        {
            red[i].se -= kol[j].fi;
            ans[mp(i,j)] += kol[j].fi;
            kol[j].se -= kol[j].fi;
            kol[j].fi=0;
        }
    }
    int j = 0;
    for(int i = 0; i < n; i++)
    {
        if(red[i].se > 0)
        {
            if(red[i].se>kol[j].se)
            {
                red[i].se-=kol[j].se;
                ans[mp(i,j)]+= kol[j].se;
                kol[j].se=0;
                j++;
                i--;
            }
            else{
                kol[j].se-=red[i].se;
                ans[mp(i,j)]+=red[i].se;
                red[i].se=0;
            }
        }
    }
    cout << s << "\n";
    cout << ans.size() << "\n";
    for(auto x : ans)
    {
        cout << x.fi.fi+1 << " " << x.fi.se+1 << " " << x.se << "\n";
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    red.resize(n);
    kol.resize(m);
    long long s1=0,s2=0;
    for(int i = 0; i < n; i++) {cin >> red[i].fi >> red[i].se; s1 += red[i].se;}
    for(int i = 0; i < m; i++) {cin >> kol[i].fi >> kol[i].se; s2 += kol[i].se;}
    if(s1<s2)solve(n,m,red,kol);
    else solve(m,n,kol,red);
}