#include <iostream>
using namespace std;
#define MaxN 503
struct granica {
    int najvise;
    int najmanje;
};
int main()
{
    int n, m, l = 0, red = 0, kolona = 0, k = 0, s = 0;
    cin >> n;
    cin >> m;
    int a[MaxN][MaxN];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            a[i][j] = 0;
        }
    }
    granica b[MaxN];
    granica c[MaxN];
    for (int i = 0; i < n; i++)
    {
        cin >> b[i].najmanje >> b[i].najvise;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> c[i].najmanje >> c[i].najvise;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            red += a[i][j];
        }
        while (l < m)
        {
            for (int j = 0; j < m; j++)
            {
                for (int z = 0; z < n; z++)
                {
                    kolona += a[j][z];
                }
                if (b[i].najvise > red && kolona < c[j].najvise)
                {
                    a[i][j]++;
                    red++;
                }
                else
                {
                    l++;
                }
                kolona = 0;
            }
        }
        red = 0;
        l = 0;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            s += a[i][j];
            if (a[i][j] != 0)
            {
                k++;
            }
        }
    }
    cout << s << endl;
    cout << k << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (a[i][j] != 0)
            {
                cout << i + 1 << " " << j + 1 << " " << a[i][j];
                cout << endl;
            }
        }
    }
    return 0;
}