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

using ll = long long;

int main()
{
    /* resenje za  45 poena */

    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];

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

    // 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>> redovi_min;
    for (int i = 0; i < n; i++) {
        redovi_min.push({-a[i], i});
    }

    ll treba;
    int red_ind;
    for (int i = 0; i < m; i++) {
        // obilazimo kolone i u svaku namestamo sumu c
        red_ind = redovi_min.top().second;

        treba = c[i] - suma_kol[i];

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

            polje[red_ind][i] += treba;
        }
        else {
            treba = b[red_ind] - suma_red[red_ind];
            suma_red[red_ind] += treba;
            suma_kol[i] += treba;

            polje[red_ind][i] += treba;

            redovi_min.pop();
        }
    }

    // sada pokusavamo da povecamo sume u kolonama

    priority_queue<pair<ll, int>> kolone_min;
    for (int i = 0; i < m; i++) {
        kolone_min.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
            kol_ind = kolone_min.top().second;

            if (moze + suma_kol[kol_ind] <= d[kol_ind]) {
                suma_kol[kol_ind] += moze;
                suma_red[i] += moze;

                polje[i][kol_ind] += moze;
            }
            else {
                moze = d[kol_ind] - suma_kol[kol_ind];
                suma_kol[kol_ind] += moze;
                suma_red[i] += moze;

                polje[i][kol_ind] += moze;

                kolone_min.pop();
            }
        }
    }

    /* generisanje odgovora */

    ll suma = 0;
    vector<pair<pair<int, int>, ll>> ans;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (polje[i][j]) {
                ans.push_back({{i, j}, polje[i][j]});
                suma += polje[i][j];
            }
        }
    }

    cout << suma << "\n";
    cout << ans.size() << "\n";
    for (auto &p : ans) {
        cout << p.first.first + 1 << " " << p.first.second + 1 << " " << p.second << "\n";
    }


    return 0;
}