#include <iostream>
#include <list>
int niz[100000][3];
int kol[50000];
int k[100000][2];
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int ukupniKrompiri;
    int pocetanRed;
    for (int i = 0; i < n+m; i++)
    {
        kol[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++)
    {
        pocetanRed = 0;
        for (int j = n; j < n+m; j++)
        {
            int maks=k[i][1];
            if (k[j][1] < maks) {
                maks = k[j][1];
            }
            if (k[j][0] <= maks && k[i][0] <= maks && pocetanRed+maks<=k[i][1] && kol[j]+maks<=k[j][1])
            {
                niz[poljaSadjena][0] = i+1;
                niz[poljaSadjena][1] = j+1-n;
                niz[poljaSadjena][2] = maks;
                maksKrompira += maks;
                poljaSadjena += 1;
                kol[j] += maks;
                pocetanRed += 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;
    }
}