#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

            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;
        }
    }

    long long suma=0, broj=0;

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

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

    return 0;
}

/*

3 3
3 4
2 3
2 4
1 2
1 4
3 4

*/