#include <bits/stdc++.h>
#define ll long long

using namespace std;

struct tp33 {
    int x;
    int y;
    int n2;
};


void solve() {
    int n, m;
    cin >> n >> m;
    int a[n];
    int b[n];
    int c[m];
    int d[m];
    pair<int, int> Novi[n];
    pair<int, int> Movi[n];

    for (int i=0;i<n;i++) {
        int ax, bx;
        cin >> ax >> bx;
        a[i] = ax;
        //Novi[i] = make_pair(ax, i);
        b[i] = bx;
    }
    for (int i=0;i<m;i++) {
        int cx, dx;
        cin >> cx >> dx;
        //Movi[i] = make_pair(bx, i);
        c[i] = cx;
        d[i] = dx;
    }
    //sort(Novi, Novi+n);
    //reverse(Novi, Novi+n);
   // sort(Movi, Movi+m);
    //reverse(Movi, Movi+m);
    if (n == 1) {
        int available = b[0];
        for (int i = 0;i<m;i++) {
            available -= c[i];
        }
        for (int i=0;i<m;i++) {
            if (c[i] + available >= d[i]) {
                cout << d[i] << " ";
                available -= (d[i]-c[i]);
            }
            if (c[i] + available < d[i]) {
                cout << c[i] + available << " ";
                available = 0;
            }
        }
        return;
    }
    //for (int i = 0;i<n;i++) {
    //    int available = Novi[i];
     //
    //}
    vector <int> vx;
    vector <int> vy;
    vector <int> vn;
    int ans = 0;
    for (int i=0;i<n;i++) {
        int available = b[i];
        for (int j=0;j<m;j++) {
            if (available < d[j]) {
                d[j] -= available;
                if (available != 0) {
                    vx.push_back(i+1);
                    vy.push_back(j+1);
                    vn.push_back(available);
                }

                ans += available;
                available = 0;
            }
            if (available >= d[j]) {
                if (d[j] != 0) {
                        vx.push_back(i+1);
                    vy.push_back(j+1);
                    vn.push_back(d[j]);
                }

                ans += d[j];
                available -= d[j];
                d[j] = 0;
            }
        }
    }
    cout << ans << "\n";
    cout << vx.size() << "\n";
    for (int i=0;i<vx.size();i++) {
        cout << vx[i] << " " << vy[i] << " " << vn[i] << "\n";
    }
}


int main()
{
    solve();
    //cout << "Hello world!" <<endl;
    return 0;
}