#include <iostream>
#include <vector>
using namespace std;

void popuni_kol(int& punih_kolona, int red, vector<int> min_red, vector<int> max_red, vector<int>& popunjeno_red, vector<int> min_kol, vector<int> max_kol, vector<int>& popunjeno_kol,int& br_krompira ,int& br_polja, vector<vector<int>>& resenje) {
    int m = popunjeno_kol.size();
    for (int j = punih_kolona; j < m; j++) {
        int pos = min(max_red[red] - popunjeno_red[red], max_kol[j] - popunjeno_kol[j]);
        if (min_red[red] <= pos && min_kol[j] <= pos) {
            br_krompira += pos;
            resenje[br_polja][0] = red+1;
            resenje[br_polja][1] = j+1;
            resenje[br_polja][2] = pos;
            br_polja++;
            popunjeno_red[red] += pos;
            popunjeno_kol[j] += pos;
            if (popunjeno_kol[j] == max_kol[j]) {
                punih_kolona++;
                return;
            }
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n), p_red(n), c(m), d(m), p_kol(m);
    for (int i = 0; i < n; i++) {
        int l, u;
        cin >> l >> u;
        a[i] = l;
        b[i] = u;
    }
    vector<vector<int>> resenje(n * m, vector<int>(3));
    int br_k = 0;
    int br_p = 0;
    for (int i = 0; i < m; i++) {
        int l, u;
        cin >> l >> u;
        c[i] = l;
        d[i] = u;
    }
    int punih_k = 0;
    for (int i = 0; i < n; i++) {
        popuni_kol(punih_k, i, a, b, p_red, c, d, p_kol,br_k,br_p,resenje);
    }
    cout << br_k << endl;
    cout << br_p << endl;
    for (int i = 0; i < br_p; i++) {
        cout << resenje[i][0] << " " << resenje[i][1] << " " << resenje[i][2] << endl;
    }
}