#include <bits/stdc++.h>

using namespace std;

void validan(int n, vector<int>& a, vector<int>& b, vector<int>& validni, int maxx);

int main(){
    int n, m; cin >> n >> m;
    vector<int> nmin(n), nmax(n), mmin(m), mmax(m), validni(m);
    for(int i = 0; i < n; i++) cin >> nmin[i] >> nmax[i];
    for(int i = 0; i < m; i++) cin >> mmin[i] >> mmax[i];
    if(n == 1){
        int s = 0, b = 0;
        for(int i = 0 ; i < m; i++){
            if(mmax[i] > 0){
                s += mmax[i];
                b++;
            }
        }
        cout << min(s, nmax[0]) << endl;
        cout << b << endl;
        if(min(s, nmax[0]) == s){
            cout << b << endl;
            for(int j = 0; j < m; j++) if(mmax[j] > 0) cout << 1 << " " << j + 1 << " " << mmax[j] << endl;
        }
        else{
            validan(m, mmin, mmax, validni, nmax[0]);
            for(int j = 0; j < m; j++) if(validni[j] > 0) cout << 1 << " " << j + 1 << " " << validni[j] << endl;
        }
    }
    return 0;
}

void validan(int n, vector<int>& a, vector<int>& b, vector<int>& validni, int maxx){
    int z = 0, r;
    for(int i = 0; i < n; i++){
        z += a[i];
        validni[i] = a[i];
    }
    for(int i = 0; i < n; i++){
        r = b[i] - a[i];
        if(z + r < maxx){validni[i] = b[i]; z += r;}
        if(z + r == maxx){validni[i] = b[i]; return;}
        if(z + r > maxx){validni[i] = a[i] + maxx - z; return;}
    }
    return;
}