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

    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= M; j++)
        {
            if(a[i] != 0 && b[i] != 0 && c[j] != 0 && d[j] != 0) cout << i << " " << j << " " << max(min(a[i], c[j]), min(b[i], d[j])) << endl;
        }
    }
    return 0;
}