#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<pair<ll, ll>> red(n);
    vector<pair<ll, ll>> kol(m);

    for (int i = 0; i < n; i++)
        cin >> red[i].first >> red[i].second;

    for (int i = 0; i < m; i++)
        cin >> kol[i].first >> kol[i].second;

    ll polje[n][m];

    if (n == 1) {
        ll sum = 0;
        for (int i = 0; i < m; i++) {
            polje[0][i] = kol[i].first;
            sum += polje[0][i];
        }
        ll ostalo = red[0].second - sum;

        if (ostalo) {
            ll dodaj;
            for (int i = 0; i < m; i++) {
                if (ostalo <= 0) break;

                dodaj =  min(kol[i].second - polje[0][i], ostalo);
                polje[0][i] += dodaj;
                ostalo -= dodaj;
            }
        }

    }
  /*  else if (red[0].first == kol[0].first && red[0].first == 0){
        // ai = ci = 0
        continue;
    } */

    /* 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;
}