#include <bits/stdc++.h>

using namespace std;

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

int n, m;
int a[maxn], b[maxn], c[maxm], d[maxm];

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

    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
    }
    for(int i = 1; i <= m; i++)
    {
        cin >> c[i] >> d[i];
    }
    int maxp = 0;
    vector<int> x, y, n1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(min(b[i], d[j]) != 0)
            {
                n1.push_back(min(b[i], d[j]));
                x.push_back(i);
                y.push_back(j);
                maxp += min(b[i], d[j]);
            }
        }
    }

    cout << maxp << endl << n1.size() << endl;
    for(int i = 0; i < n1.size(); i++)
    {
        if(n1[i] != 0) cout << x[i] << " " << y[i] << " " << n1[i] << endl;
    }

    return 0;
}