#include <iostream>

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

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

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

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

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

    return 0;
}