#include <iostream>
#include <map>
#define forn(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int oo = 2e5+7;
int n, m;
ll a[oo], b[oo], c[oo], d[oo];
ll sn[oo], sm[oo];
map<pair<int, int>, ll> ans;
void add(int x, int y, ll c) {
    if (ans.count({x, y}) == 0) ans[{x, y}] = c;
    else ans[{x, y}] += c;

    sn[x] += c;
    sm[y] += c;
}
ll get(int x, int y) {
    if (ans.count({x, y}) == 0) return 0;
    else return ans[{x, y}];
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    forn(i, n) cin >> a[i] >> b[i];
    forn(i, m) cin >> c[i] >> d[i];
    bool fpass = true;

    int x = 0, y = 0;
    while (true) {
        ll maxf;
        if ((fpass ? a[x] : b[x]) == sn[x])
            goto nextrow;

        maxf =
        min((fpass ? a[x] : b[x]) - sn[x],
            (fpass ? c[y] : d[y]) - sm[y]);
        add(x, y, maxf);

        y++;
        if (y == m) {
            nextrow:
            y = 0;
            x++;
            if (x == n) {
                if (fpass) {
                    x = 0, y = 0;
                    fpass = false;
                } else break;
            }
        }
    }
    ll sum = 0;
    forn(i, n) sum += sn[i];
    cout << sum << endl << ans.size() << endl;
    for (auto e: ans) {
        int x = e.first;
        int y = e.second;
        cout << x + 1 << " " << y + 1 << " " << get(x, y) << endl;
    }
    /*
    forn(i, n) {
        forn(j, m) cout << get(i, j) << " ";
        cout << endl;
    }
    */
}