#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main()
{
    int n, m;
    cin >> n >> m;

    // ogranicenja
    vector<ll> a(n), b(n);
    vector<ll> 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];

    sort(d.begin(), d.end());

 /*   ll polje[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            polje[i][j] = 0; */

    ll sum = 0;
    map<pair<int, int>, int> ans;

    // sume;
    vector<ll> suma_red(n, 0); // suma u i-tom redu;
    vector<ll> suma_kol(m, 0); // suma u i-toj koloni

    priority_queue<pair<ll, int>> pq;
    for (int i = 0; i < n; i++) {
        pq.push({-b[i], i});
    }

    int red_ind;
    for (int i = 0; i < m; i++) {
        // obilazimo kolone i u svaku namestamo sumu c
        if (pq.size() == 0) break;

        red_ind = pq.top().second;

        if (c[i] <= b[red_ind]) {
            suma_red[red_ind] += c[i];
            suma_kol[i] = c[i];

           ans[{red_ind, i}] += c[i];
           sum += c[i];

            if (suma_red[red_ind] == b[red_ind]) {
                pq.pop();
            }
        }
        else {
            ll smanji;
            while(c[i] && pq.size()) {
                smanji = min(b[red_ind], c[i]);
                c[i] -= smanji;
                suma_red[red_ind] += smanji;
                suma_kol[i] += smanji;
                ans[{red_ind, i}] += smanji;
                sum += smanji;
                if (suma_red[red_ind] == b[red_ind]) {
                    pq.pop();
                    if (pq.size())
                        red_ind = pq.top().second;
                }
            }
        }

    }

    /* for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << polje[i][j] << " ";
        }
        cout << "\n";
    } */

    // sada pokusavamo da povecamo sume u kolonama

    while(!pq.empty()) pq.pop();

    for (int i = 0; i < m; i++) {
        pq.push({-d[i], i});
    }

    ll moze;
    int kol_ind;
    for (int i = 0; i < n; i++) {
        moze = b[i] - suma_red[i];

        if (moze > 0) {
            // u i-ti red mozemo dodati jos krompira
            if(pq.size() == 0) break;

            kol_ind = pq.top().second;

            if (moze <= d[kol_ind]) {
                // mozemo sve staviti u ovu kolonu
                suma_kol[kol_ind] += moze;
                suma_red[i] += moze;

               ans[{i, kol_ind}] += moze;
               sum += moze;

                if (suma_kol[kol_ind] == d[kol_ind]) {
                    pq.pop();
                }
            }
            else {
                ll smanji;
                while(moze && pq.size()) {
                    smanji = min(moze, d[kol_ind] - suma_kol[kol_ind]);
                    moze -= smanji;
                    suma_kol[kol_ind] += smanji;
                    suma_red[i] += smanji;
                    sum += smanji;
                    ans[{i, kol_ind}] += smanji;
                    if (suma_kol[kol_ind] == d[kol_ind]) {
                        pq.pop();
                        if (pq.size())
                            kol_ind = pq.top().second;
                    }
                }
            }
        }
    }

    int cnt = 0;
    for (auto it = ans.begin(); it != ans.end(); it++) {
        if (it->second == 0) ++cnt;
    }

    cout << sum << "\n";
    cout << ans.size() - cnt << "\n";
    for (auto &p : ans) {
        if (p.second == 0) continue;
        cout << p.first.first + 1 << " " << p.first.second + 1 << " " << p.second << "\n";
    }

    /* for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << polje[i][j] << " ";
        }
        cout << "\n";
    } */

    /* generisanje odgovora */



    return 0;
}