#include <bits/stdc++.h>

using namespace std;

struct ans{
    int x, y;
    long long n;

    ans(int x, int y, long long n) : x(x), y(y), n(n) {}
};

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m;

    cin >> n >> m;

    vector<pair<long long, long long>> r(n), c(m);
    long long maxR = 0, maxC = 0;

    for(int i = 0; i < n; ++i) cin >> r[i].first >> r[i].second, maxR += r[i].second;
    for(int i = 0; i < m; ++i) cin >> c[i].first >> c[i].second, maxC += c[i].second;
    long long maxP = min(maxR, maxC), oh = maxP;

    vector<ans> av;
    if(maxR > maxC){
        vector<long long> chr(n), chc(m);

        for(int i = 0; i < n; ++i) oh -= r[i].first;
        int ri = 0, ci = 0;
        long long tans = 0;

        while(ri < n){
            long long cans = min(r[ri].first - chr[ri], c[ci].second - chc[ci]);
            long long extra = min(oh, min(r[ri].second - cans - chr[ri], c[ci].second - cans - chc[ci]));
            tans += cans + extra;
            chr[ri] += cans + extra;
            chc[ci] += cans + extra;
            oh -= extra;
            if(chr[ri] >= r[ri].first && extra == 0){
                if(tans != 0) av.emplace_back(ri + 1, ci + 1, tans);
                tans = 0;
                ++ri;
            }
            if(chc[ci] == c[ci].second){
                if(tans != 0) av.emplace_back(ri + 1, ci + 1, tans);
                tans = 0;
                ++ci;
            }
        }
    }else{
        vector<long long> chr(n), chc(m);

        for(int i = 0; i < m; ++i) oh -= c[i].first;
        int ri = 0, ci = 0;
        long long tans = 0;

        while(ci < m){
            long long cans = min(max(0LL, c[ci].first - chc[ci]), max(0LL, r[ri].second - chr[ri]));
            long long extra = min(oh, min(max(0LL, c[ci].second - cans - chc[ci]), max(0LL, r[ri].second - cans - chr[ri])));
            assert(oh >= 0);
            assert(cans >= 0);
            assert(extra >= 0);
            tans += cans + extra;
            chr[ri] += cans + extra;
            chc[ci] += cans + extra;
            oh -= extra;

            if(chr[ri] == r[ri].second){
                if(tans != 0) av.emplace_back(ri + 1, ci + 1, tans);
                tans = 0;
                ++ri;
            }

            if(chc[ci] >= c[ci].first && extra == 0){
                if(tans != 0) av.emplace_back(ri + 1, ci + 1, tans);
                tans = 0;
                ++ci;
            }
        }
    }

    cout << maxP << '\n' << av.size() << '\n';
    for(int i = 0; i < (int) av.size(); ++i) cout << av[i].x << ' ' << av[i].y << ' ' << av[i].n << '\n';

    return 0;
}