#include <bits/stdc++.h>

using namespace std;

void Njedan(int M, int a, int bp, int c[], int d[]){
    int p, b = bp;
    int niz[M];
    for(int i = 0; i < M; i++){
        niz[i] = c[i];
        a -= c[i];
        b -= c[i];
    }
/*
    cout << endl << endl;
    for(int i = 0; i < M; i++){
            cout << i + 1 << ": " << niz[i] << endl;
    }
*/

    for(int i = 0; i < M; i++){
        p = d[i] - c[i];
        if(p < b){
            b -= p;
            niz[i] = d[i];
        }
        else{

/*
    cout << endl << 2 << endl;
    for(int i = 0; i < M; i++){
            cout << i + 1 << ": " << niz[i] << endl;
    }
*/

            niz[i] += b;
            cout << bp << endl;

            int cntr = 0;
            for(int j = 0; j < M; j++){
                if(niz[j] != 0){
                    cntr ++;
                }
            }
            cout << cntr << endl;
            for(int j = 0; j < M; j++){
                if(niz[j] != 0){
                    cout << "1 " << j+1 << " " << niz[j] << endl;

                }
            }
            return;
        }

    }

/*
    cout << endl << 3 << endl;
    for(int i = 0; i < M; i++){
            cout << i + 1 << ": " << niz[i] << endl;
    }
*/

    cout << bp - b << endl;
            int cntr = 0;
            for(int j = 0; j < M; j++){
                if(niz[j] != 0){
                    cntr ++;
                }
            }
            cout << cntr << endl;
            for(int j = 0; j < M; j++){
                if(niz[j] != 0){
                    cout << "1 " << j+1 << " " << niz[j] << endl;
                    }
            }
}



int main()
{
    int N, M;
    cin >> N >> M;
    int a[N], b[N], c[M], d[M];
    for(int i = 0; i < N; i++){
        cin >> a[i] >> b[i];
    }
    for(int i = 0; i < M; i++){
        cin >> c[i] >> d[i];
    }

    if(N == 1){
        Njedan(M, a[0], b[0], c, d);
    }
    else{
        cout << 0 << endl << 0;
    }

    return 0;
}