#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define pb push_back
#define xx first
#define yy second


using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int n,m;
ll a[50005];
ll b[50005];
ll c[50005];
ll d[50005];

ll sumared[50005];
ll sumakol[50005];

map<pii,ll>kolko;

struct kv{
    int x;
    int y;
    int broj;
};

vector<kv> sol;

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> n >> m;
    ff(i,1,n)cin >> a[i] >> b[i];
    ff(i,1,m)cin >> c[i] >> d[i];

    ll s1 = 0, s2 = 0;
    ff(i,1,n)s1 += b[i];
    ff(i,1,m)s2 += d[i];

    bool dsl = 0;

    if(s1 <= s2){

    }
    else{
        swap(s1,s2);
        dsl = 1;
        ff(i,1,max(n,m)){
            swap(a[i],c[i]);
            swap(b[i],d[i]);
        }
        swap(n,m);
    }


    stack<int> prvi;
    stack<int> drugi;

    ff(i,1,n){
        if(b[i] > 0)prvi.push(i);
    }
    ff(i,1,m){
        if(c[i] > 0)drugi.push(i);
    }
    while(!drugi.empty()){
        int p1 = prvi.top();
        int p2 = drugi.top();
        prvi.pop();
        drugi.pop();
        int fali1 = b[p1] - sumared[p1];
        int fali2 = c[p2] - sumakol[p2];

        sol.pb({p1,p2,min(fali1,fali2)});
        sumared[p1] += min(fali1,fali2);
        sumakol[p2] += min(fali1,fali2);
        if(sumared[p1] < b[p1])prvi.push(p1);
        if(sumakol[p2] < c[p2])drugi.push(p2);
    }

    ff(i,1,m){
        if(sumakol[i] < d[i])drugi.push(i);
    }

    while(!prvi.empty()){
        int p1 = prvi.top();
        int p2 = drugi.top();
        prvi.pop();
        drugi.pop();
        int fali1 = b[p1] - sumared[p1];
        int fali2 = d[p2] - sumakol[p2];

        sol.pb({p1,p2,min(fali1,fali2)});
        sumared[p1] += min(fali1,fali2);
        sumakol[p2] += min(fali1,fali2);
        if(sumared[p1] < b[p1])prvi.push(p1);
        if(sumakol[p2] < d[p2])drugi.push(p2);
    }

    cout << s1 << "\n";
    int sta = sol.size();
    cout << sta << "\n";
    for(auto c:sol){
        if(dsl){
            cout << c.y << " " << c.x << " " << c.broj << "\n";
        }
        else{
            cout << c.x << " " << c.y << " " << c.broj << "\n";
        }
    }

    return 0;
}