#include <bits/stdc++.h>

using namespace std;

const int maxn = 5e4;
const int maxm = 5e4;
const long long maxabcd = 1e9;

int n, m;
long long a[maxn], b[maxn], c[maxm], d[maxm];
long long maxk = 0, maxr = 0, polja = 0;

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> n >> m;
    polja = n * m;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
        maxr += b[i];
        if(a[i] == 0 && b[i] == 0) polja -= m;
    }
    for(int i = 1; i <= m; i++)
    {
        cin >> c[i] >> d[i];
        maxk += d[i];
        if(c[i] == 0 && d[i] == 0)
        {
            polja -= n;
            if(a[i] == c[i] && b[i] == d[i]) polja++;
        }
    }

    cout << min(maxr, maxk) << endl;

    polja = min(polja, min(maxr, maxk));
    cout << polja << endl;

    int i = 0;
    while(polja--)
    {
        if(a[i / m + 1] == 0 && b[i / m + 1] == 0 || c[i % m + 1] == 0 && d[i % m + 1] == 0) i++;
        cout << i / m + 1 << " " << i % m + 1 << " " << min(b[i / m + 1], d[i % m + 1]) << endl;
        i++;
    }
    return 0;
}