#include <iostream>

using namespace std;

#define broj long long int

int main() {
    // ????? ??
    broj n, m, i, j;
    broj sC=0, sD = 0;
    cin >> n >> m;
    broj a[n], b[n], c[m], d[m];
    for(i=0; i<n; i++) {
        cin >> a[i] >> b[i];
    }
    for(j=0; j<m; j++) {
        cin >> c[j] >> d[j];
        sC += c[j];
        sD += d[j];
    }

    if(n==1) {
        if(sD <= b[0]) {
            cout << sD << endl;
            cout << m << endl;
            for(j=0; j<m; j++) {
                cout << 1 << " " << j+1 << " " << d[j] << endl;
            }
        } else {
            if(sC == b[0]) {
                cout << sC << endl;
                cout << m << endl;
                for(j=0; j<m; j++) {
                    cout << 1 << " " << j+1 << " " << c[j] << endl;
                }
            } else {
                // izmedju sC i sD
                broj stampa[m];

                cout << b[0] << endl;
                cout << m << endl;
                j = 0;
                while(b[0]>=d[j]) {
                    stampa[j] = d[j];
                    b[0] -= d[j];
                    j++;
                }
                if(b[0]>0) {
                    stampa[j] = b[0];
                    j++;
                }
                while(j<m) {
                    stampa[j]=0;
                    j++;
                }
                for(j=0; j<m; j++) {
                    cout << 1 << " " << j+1 << " " << stampa[j] << endl;
                }
            }
        }
    } else {
        // kvadratna matrica
        broj min1 = min(b[0], d[0]);
        broj max1 = n * min1;

        cout << max1 << endl;
        cout << n << endl;
        for(i=0; i<n; i++) {
                cout << i+1 << " " << i+1 << " " << min1 << endl;
        }
    }
}