#include <iostream>

using namespace std;

int main()
{
    int n, m, k = 0, k1 = 0; //k = broj polja na kojima su krompiri, k1 = broj krompira
    cin >> n >> m;
    long long mat[n][m];
    long long nizi[n * 2], nizj[m * 2];

    for (int i = 0; i < n * 2; i += 2) {
        cin >> nizi[i] >> nizi[i + 1];
    }
    for (int i = 0; i < m * 2; i += 2) {
        cin >> nizj[i] >> nizj[i + 1];
    }

    for (int i = 0; i < n; i++) {
        long long brKrompiraRed = nizi[i * 2 + 1];
        for (int j = 0; j < m; j++) {
            long long brKrompiraKolona = nizj[j * 2 + 1];
            long long br = min(brKrompiraRed, brKrompiraKolona);
            if (br > brKrompiraRed) {
                br = brKrompiraRed;
            }
            brKrompiraRed -= br;
            nizj[j * 2 + 1] -= br;
            mat[i][j] = br;
            k1 += br;
            if (br != 0) { k++; }
        }
    }

    cout << k1 << endl << k << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (mat[i][j] != 0) {
                cout << i + 1 << " " << j + 1 << " " << mat[i][j] << endl;
            }
        }
    }

    return 0;
}