#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 = 5e4+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 (c == 0) return;
    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 fpassx = true, fpassy = true;

    int tofill = 0;
    int x = 0, y = 0;
    while (true) {
        ll xb = fpassx ? a[x] : b[x];
        ll yb = fpassy ? c[y] : d[y];

        ll leftx = xb - sn[x];
        ll lefty = yb - sm[y];

        ll maxf = min(leftx, lefty);
        add(x, y, maxf);

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