#include "iostream"
#include "algorithm"
#include "cmath"
#include "map"
#include "vector"
#include "array"
using namespace std;
#define forn(i, n) for (int i = 0; i < (n); i++)
#define forns(i, s, n) for (int i = s; i < (n); i++)
#define int long long
pair<int, int> operator-=(pair<int, int> &a, int x) {
    a.first -= x;
    a.second -= x;
    return a;
}

signed main() {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> a(n);
    vector<pair<int, int>> b(m);
    map<pair<int, int>, int> pole;
    int sml = 0, smr = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second;
        sml += a[i].second;
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i].first >> b[i].second;
        smr += b[i].second;
    }
    bool rew = 0;
    if (smr > sml) {
        swap(a, b);
        swap(n, m);
        rew = 1;
    }
    int j = 0;
    for (int i = 0; i < n; ++i) {
        while (a[i].first != 0) {
            int k = min(b[j].second, a[i].first);
            b[j] -= k;
            a[i] -= k;
            if (k) pole[{i, j}] += k;
            if (b[j].second == 0)
                j++;
        }
    }
    j = 0;
    for (int i = 0; i < m; ++i) {
        while (b[i].second != 0) {
            int k = min(b[i].second, a[j].second);
            b[i] -= k;
            a[j] -= k;
            if (k) pole[{j, i}] += k;
            if (a[j].second == 0)
                j++;
        }
    }
    vector<array<int, 3>> res;
    for (auto i : pole) {
        res.push_back({i.first.first, i.first.second, i.second});
    }
    cout << min(sml, smr) << '\n';
    cout << res.size() << '\n';
    for (auto i : res) {
        if (rew) {
            cout << i[1] + 1 << ' ' << i[0] + 1 << ' ' << i[2] << '\n';
        } else {
            cout << i[0] + 1 << ' ' << i[1] + 1 << ' ' << i[2] << '\n';
        }
    }
}