#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    int a[n], b[n], c[m], d[m];
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for(int i = 0; i < m; i++) cin >> c[i] >> d[i];
    long long need;
    long long cntb = 0, cntd = 0;
    for(int i = 0; i < n; i++) cntb += b[i];
    for(int i = 0; i < m; i++) cntd += d[i];
    need = min(cntb, cntd);
    cout << need << "\n";
    vector<pair<pair<int, int>, long long>> res;
    if(cntb < cntd) {
        long long x[n], y[m];
        for(int i = 0; i < n; i++) x[i] = b[i];
        long long sum = 0;
        for(int i = 0; i < m; i++) {
            y[i] = c[i];
            sum += y[i];
        }
        for(int i = 0; i < m; i++) {
            if(sum + d[i] - c[i] >= need) {
                y[i] += need - sum;
                sum = need;
                break;
            } else {
                y[i] = d[i];
                sum += d[i] - c[i];
            }
        }
//        for(int i = 0; i < n; i++) cout << x[i] << " ";
//        cout << "\n";
//        for(int i = 0; i < m; i++) cout << y[i] << " ";
//        cout << "\n";
        long long left[m];
        for(int i = 0; i < m; i++) left[i] = y[i];
        int j = 0;
        for(int i = 0; i < n; i++) {
            long long sum = 0;
            for(; j < m; j++) {
//                cout << i << " " << j << "\n";
                if(sum + left[j] >= x[i]) {
                    if(x[i] - sum > 0) res.push_back({{i, j}, x[i] - sum});
                    left[j] -= x[i] - sum;
                    sum = x[i];
                    break;
                } else {
                    if(left[j] > 0) res.push_back({{i, j}, left[j]});
                    sum += left[j];
                    left[j] = 0;
                }
            }
        }
    } else {
        long long x[n], y[m];
        for(int i = 0; i < m; i++) y[i] = d[i];
        long long sum = 0;
        for(int i = 0; i < n; i++) {
            x[i] = a[i];
            sum += x[i];
        }
        for(int i = 0; i < n; i++) {
            if(sum + b[i] - a[i] >= need) {
                x[i] += need - sum;
                sum = need;
                break;
            } else {
                x[i] = b[i];
                sum += b[i] - a[i];
            }
        }
        long long left[n];
        for(int i = 0; i < n; i++) left[i] = x[i];
        int i = 0;
        for(int j = 0; j < m; j++) {
            long long sum = 0;
            for(; i < n; i++) {
                if(sum + left[i] >= y[j]) {
                    if(y[j] - sum > 0) res.push_back({{i, j}, y[j] - sum});
                    left[i] -= y[j] - sum;
                    sum = y[j];
                    break;
                } else {
                    if(left[i] > 0) res.push_back({{i, j}, left[i]});
                    sum += left[i];
                    left[i] = 0;
                }
            }
        }
    }
    cout << res.size() << "\n";
    for(int i = 0; i < res.size(); i++) cout << res[i].first.first + 1 << " " << res[i].first.second + 1 << " " << res[i].second << "\n";
    return 0;
}
/*
2 2
1 3
0 0
1 2
1 1

*/