#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n), c(m), d(m);
    ll sa, sb;
    sa = sb = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        sa += b[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> c[i] >> d[i];
        sb += d[i];
    }
    bool ch = 0;
    if (sa > sb) {
        swap(sa, sb);
        swap(n, m);
        swap(a, c);
        swap(b, d);
        ch = 1;
    }
    map<pair<int, int>, int> mp;
    int i, j;
    i = j = 0;
    while (i < n && j < m) {
        int o = min(b[i], c[j]);
        b[i] -= o;
        c[j] -= o;
        d[j] -= o;
        if (o > 0) mp[{i, j}] += o;
        if (b[i] == 0) i += 1;
        if (c[j] == 0) j += 1;
    }
    priority_queue<pair<int, int>> pq;
    for (int i = 0; i < m; i++) pq.push({d[i], i});
    for (int i = 0; i < n; i++) {
        while (!pq.empty()) {
            int j = pq.top().second; pq.pop();
            int o = min(b[i], d[j]);
            b[i] -= o;
            d[j] -= o;
            if (o > 0) mp[{i, j}] += o;
            if (d[j] > 0) {
                pq.push({d[j], j});
                break;
            }
        }
    }
    cout << sa << en;
    cout << mp.size() << en;
    for (auto u : mp) {
        if (ch == 0) cout << u.first.first + 1 << sp << u.first.second + 1 << sp << u.second << en;
        else cout << u.first.second + 1 << sp << u.first.first + 1 << sp << u.second << en;
    }
    return 0;
}