#include <bits/stdc++.h>

using namespace std;

int ma (int x, int y) {

    if (x > y) { return x; }
    return y;

}

int mi (int x, int y) {

    if (x < y) { return x; }
    return y;

}

int main () {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    int mr[n];

    int x, y;

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

        cin >> x >> y;

        mr[i] = ma(x, y);

    }

    int mk[m];
    int mkol_mi[m];

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

        cin >> x >> y;

        mk[i] = ma(x, y);
        mkol_mi[i] = mi(x, y);

    }

    int br_num = 0;
    int br_sum = 0;

    vector<int> br_num_arr;
    vector<int> br_num_row;
    vector<int> br_num_col;

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

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

            int mkol;
            if (j != m-1) {

                 mkol = mkol_mi[j+1];

            }
            else { mkol = 0; }

            int br = mi(mr[i], mk[j]) - mkol;

            if (br < 1) { continue; }

            mr[i] -= br;
            mk[j] -= br;

            if (mr[i] < 0 || mk[i] < 0) { break; }

            br_num++;
            br_sum += br;
            br_num_arr.push_back(br);
            br_num_row.push_back(i+1);
            br_num_col.push_back(j+1);

        }

    }

    cout << br_sum << "\n";
    cout << br_num << "\n";

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

        cout << br_num_row[i] << " " << br_num_col[i] << " " << br_num_arr[i] << "\n";

    }

    return 0;

}