#include <bits/stdc++.h>

using namespace std;

int main(){
    int n, m; cin >> n >> m;
    vector<int> nmin(n), nmax(n), mmin(m), mmax(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;
        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;
        }
    }
    return 0;
}