#include <iostream>
#include <list>
int niz[100000][3];
int red[50000];
int kol[50000];
int k[100000][2];
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n+m; i++)
    {
        if(i>n)
        kol[i] = 0;
        else
        red[i] = 0;
        int a, b;
        cin >> k[i][0] >> k[i][1];
    }
    int maksKrompira=0;
    int poljaSadjena=0;
    for (int i = 0; i < n; i++)
    {
        if (!(red[i] >= k[i][0]) && red[i] < k[i][1])
            for (int j = n; j < n + m; j++)
            {
                if (!(kol[j - n] >= k[j][0]) && kol[j - n] < k[j][1]) {
                    int min = k[i][0];
                    if (k[j][0] < min) {
                        min = k[j][0];
                    }

                    if (red[i] + min <= k[i][1] && kol[j - n] + min <= k[j][1])
                    {
                        niz[poljaSadjena][0] = i + 1;
                        niz[poljaSadjena][1] = j + 1 - n;
                        niz[poljaSadjena][2] = min;
                        maksKrompira += min;
                        poljaSadjena += 1;
                        kol[j - n] += min;
                        red[i] += min;
                    }
                }
            }
        if(!(red[i] == k[i][1]))
        for (int j = n; j < n+m; j++)
        {
            if (!(kol[j - n] == k[j][1])) {
                int maksdodavanje = k[i][1];
                int maks = k[i][1]-red[i];
                if (k[j][1]-kol[j] < maks) {
                    maksdodavanje = k[j][1];
                    maks = k[j][1]- kol[j];
                }
                if (maks > 0 && k[j][0] <= maksdodavanje && k[i][0] <= maksdodavanje && red[i] + maks <= k[i][1] && kol[j - n] + maks <= k[j][1])
                {
                    bool same = false;
                    for (int k = 0; k < poljaSadjena; k++)
                    {
                        if (niz[k][0] == i + 1 && niz[k][1] == j + 1 - n)
                        {
                            same = true;
                            niz[k][2] += maks;
                            break;
                        }
                    }
                    if (!same) {
                    niz[poljaSadjena][0] = i + 1;
                    niz[poljaSadjena][1] = j + 1 - n;
                    niz[poljaSadjena][2] = maks;
                    poljaSadjena += 1;
                    }
                    maksKrompira += maks;
                    kol[j - n] += maks;
                    red[i] += maks;
                }
            }
        }
    }
    cout << maksKrompira << endl;
    cout << poljaSadjena << endl;
    for (int i = 0; i < poljaSadjena; i++)
    {
        cout << niz[i][0] << " " << niz[i][1] << " " << niz[i][2]<<endl;
    }
}