#include <iostream>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
    long long n, m, i, j, mini, maxi, res = 0, br_polja = 0;
    cin >> n >> m;
    long long a[n], b[n];
    long long c[m], d[m];
    long long resx[n * m], resy[n * m], resk[n * m];
    for(i = 0; i < n; i ++){
        cin >> a[i] >> b[i];
    }
    for(j = 0; j < m; j ++){
        cin >> c[j] >> d[j];
    }
    for(i = 0; i < n; i ++){
        for(j = 0; j < m; j ++){
            mini = min(b[i], d[j]);
            maxi = max(a[i], c[j]);
            if(maxi <= mini){
                resx[br_polja] = i + 1;
                resy[br_polja] = j + 1;
                resk[br_polja] = mini;
                res += mini;
                br_polja ++;
                b[i] -= mini;
                d[j] -= mini;
                if(b[i] == 0) break;
            }
        }
    }
    cout << res << endl << br_polja << endl;
    for(i = 0; i < br_polja; i ++){
        cout << resx[i] << " " << resy[i] << " " << resk[i] << endl;
    }
    return 0;
}