#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);
    vector<pair<int, int>> coors(n * 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;
                    newMax = newMax - colL[j].second + colL[j].first;
                }
                else if(newMax > 0)
                {
                    cout << 1 << " " << j << " " << colL[j].first + newMax;
                }
                else
                {
                    cout << 1 << " " << j << " " << colL[j].first;
                }
        }
    }

    /*
    maxp -= minp;

    
    for (int i = 0; i < n; i++)
    {
        if (rowL[i].second != 0)
        {
            for (int j = 0; j < m; j++)
            {
                if (colL[j].second != 0)
                {
                    if(rowL[i].second > colL[j].second)

                }
            }
        }
        if (maxp == 0) i = n;
    }
    
    */
    /*
    int spent = 0;
    for (int i = 0; i < n; i++)
    {
        int allowence = max - minRVals + 1 - spent;
        int allow = max - minRVals + 1 - spent;
        int ccVals = minCVals;
        for (int j = 0; j < m; j++)
        {
            int writeOut;
            if (allow - colL[j].second >= ccVals)
            {
                writeOut = colL[j].second;
                ccVals -= colL[j].second;
                allow -= colL[j].second;
            }
            else
            {
                int spending = allow - ccVals;
                writeOut = spending;
                ccVals -= spending;
                allow -= spending;
            }
            if (j == m - 1) cout << writeOut << '\n';
            else cout << writeOut << " ";
        }
        spent += allowence - allow;
        minRVals -= rowL[i].first;
    }*/

    return 0;
}