#include <iostream>

using namespace std;

int main()
{
    int n, m;

    cin >> n >> m;

    int niz[m];

    int a[n], b[n];

    int c[m], d[m];

    for(int i = 0; i < n; i++){

        cin >> a[i] >> b[i];
    }

    long long zc = 0;

    for(int i = 0; i < m; i++){

        cin >> c[i] >> d[i];

        zc += c[i];

        niz[i] = c[i];

    }

    int maks = 0;
    int k = 0;

    if(n == 1){
    b[0] -= zc;

    for(int i = 0; i < m; i++){

        if(d[i] - c[i] < b[0]){

            niz[i] = d[i];

            b[0] -= d[i] - c[i];
        }

        else{

            niz[i] += b[0];

            b[0] = 0;

        }

        if(niz[i] > 0) k++;

        maks += niz[i];

    }


    cout << maks << endl;

    cout << k <<  endl;

    for(int i = 0; i < m; i++){

        if(niz[i] > 0) cout << 1 << " " << i + 1 << " " << niz[i] << endl;

    }

    }

    else if(n == m){
        
        maks = 0;
        
        k = 0;
        
        for(int i = 0; i < n; i++){

            maks += b[i];

            if(b[i] > 0) k++;
        }
        
        cout << maks << endl;
        
        cout << k << endl;

        for(int i = 0; i < n; i++){

            if(b[i] > 0)cout << i+1 << " " << i+1 << " " << b[i] <<  endl;
        }
    }

    return 0;
}