#include <bits/stdc++.h>

using namespace std;


 int  res[20000][20000] = {0};

struct vred{
    int malo;
    int mnogo;
    int sada;
};

bool mogu (vred a, vred b)
{
    if (a.sada + 1 > a.mnogo || b.sada + 1 > b.mnogo){
        return false;
    }
    return true;
}



int main()
{
    int n, m;
    cin >> n >> m;
    vred x[50000];
    vred y [50000];




    int a, b;
    int zbirx = 0, zbiry = 0;

    for (int i = 0; i < n; i++){
        cin >> a >> b;
        zbirx = zbirx + b;
        x[i].malo = a;
        x[i].mnogo = b;
        x[i].sada = 0;
    }

    for (int i = 0; i < m; i++){
        cin >> a >> b;
        zbiry = zbiry + b;
        y[i].malo = a;
        y[i].mnogo = b;
        y[i].sada = 0;
    }

    int rez = min(zbirx, zbiry);
    cout << rez << "\n";
    bool first_time = true;
    if (zbiry <= zbirx)
    {
        for (int j = 0; j < m; j++){
                first_time = true;
             while (y[j].mnogo > y[j].sada){
            for (int i = 0; i < n; i++){
                   if (mogu(x[i], y[j])){

                    x[i].sada++;
                    y[j].sada++;
                    res[i][j]++;
                    //cout << i << ", " << j << "   " << res[i][j] << "\n";
                }

            }
             }
        }
    }
    else {
    for (int i = 0; i < n; i++){
            while (x[i].mnogo > x[i].sada){
        for (int j = 0; j < m; j++){

                if (mogu(x[i], y[j])){
                    x[i].sada++;
                    y[j].sada++;
                    res[i][j]++;
                   // cout << i << ", " << j << "   " << res[i][j] << "\n";
                }
            }
            }
            }

    }


    int counter = 0;

    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            if (res[i][j]!= 0){
                counter++;
            }
        }

    }
    cout << counter << "\n";

    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            if (res[i][j] == 0){
                continue;
            }
            cout << i + 1 << " " << j + 1 <<  " " << res[i][j] << "\n";
        }

    }
}