#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, m, i, j;
    cin >> n >> m;

    vector<int> a(n), b(n);
    for (i=0;i<n;i++) {
        cin >> a[i] >> b[i];
    }

    vector<int> c(m), d(m);
    for (i=0;i<m;i++) {
        cin >> c[i] >> d[i];
    }

    vector<vector<int>> krompiri(n, vector<int>(m, 0));

    int max_krompiri = 0;
    for (i=0;i<n;i++) {
        for (j=0;j<m;j++) {
            int min_krompiri = max(a[i], c[j]);
            int max_krompiri_polje = min(b[i], d[j]);
            if (min_krompiri <= max_krompiri_polje) {
                krompiri[i][j] = max_krompiri_polje;
                max_krompiri += krompiri[i][j];
            }
        }
    }

    cout << max_krompiri << endl;
    int broj_polja = 0;
    for (i=0;i<n;i++) {
        for (j=0;j<m;j++) {
            if (krompiri[i][j] > 0) {
                broj_polja++;
            }
        }
    }
    cout << broj_polja << endl;
    for (i=0;i<n;i++) {
        for (j=0;j<m;j++) {
            if (krompiri[i][j] > 0) {
                cout << i + 1 << " " << j + 1 << " " << krompiri[i][j] << endl;
            }
        }
    }

    return 0;
}