#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    int maxRSum = 0;
    int minRVals = 0;
    vector<pair<int, int>> rowL(n);
    vector<pair<int, int>> colL(m);
    int zCount = 0;
    int kn = 0;
    for (int i = 0; i < n; i++)
    {
        int a, b; cin >> a >> b;
        rowL[i] = make_pair(a, b);
        maxRSum += b;
        minRVals += a;
        if (b == 0) kn++;

    }
    int km = 0;
    int maxCSum = 0;
    int minCVals = 0;
    for (int i = 0; i < m; i++)
    {
        int a, b; cin >> a >> b;
        colL[i] = make_pair(a, b);
        maxCSum += b;
        minCVals += a;
        if (b == 0) km++;
    }
    int k = n * m - kn * m - km * n + kn * km;
    int maxp = min(maxRSum, maxCSum);
    int minp = max(minRVals, minCVals);
    cout << maxp << '\n';
    cout << k << '\n';

    
    if (n == 1)
    {
        int newMax = maxp - minCVals;
        for (int j = 0; j < m; j++)
        {

            if (colL[j].second != 0)
                if (colL[j].second - newMax <= 0)
                {
                    cout << 1 << " " << j << " " << colL[j].second << '\n';
                    newMax = newMax - colL[j].second + colL[j].first;
                }
                else if(newMax > 0)
                {
                    cout << 1 << " " << j << " " << colL[j].first + newMax << '\n';
                }
                else
                {
                    cout << 1 << " " << j << " " << colL[j].first << '\n';
                }
        }
    }
    else
    {

    }

    return 0;
}