#include <bits/stdc++.h>

using namespace std;

int maks(int a) {
    if(a < 0) return 0;
    else return a;
}

int main()
{
    int n, m;
    cin >> n >> m;

    int a[n], b[n];
    for(int i = 0; i < n; i++)
        cin >> a[i] >> b[i];

    int c[m], d[m];
    for(int i = 0; i < m; i++)
        cin >> c[i] >> d[i];

    int temp;
    int matrica[n][m];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(b[i] < c[j]) {
                temp = b[i];
            }
            else if(d[j] < a[i]) {
                temp = d[j];
            }
            else if(b[i] < d[j]) {
                temp = b[i];
            }
            else {
                temp = d[j];
            }

            matrica[i][j] = temp;
            a[i] = maks(a[i] - temp);
            b[i] = b[i] - temp;
            c[j] = maks(c[j] - temp);
            d[j] = d[j] - temp;
        }
    }

    int suma=0, broj=0;

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(matrica[i][j] > 0)
            {
                suma+=matrica[i][j];
                broj++;
            }
        }
    }

    cout << suma << endl << broj << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(matrica[i][j] > 0) cout << i+1 << " " << j+1 << " " << matrica[i][j] << " " << endl;
        }
    }

    return 0;
}