#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n, m;
    vector<long> one, two, three;
    long long maks = 0, br = 0;
    cin >> n >> m;
    pair<int, int> a[n], b[m], aa[n], bb[m];
    for(int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
        aa[i].first = a[i].second;
        aa[i].second = i + 1;
    }
    for(int i = 0; i < m; i++) {
        cin >> b[i].first >> b[i].second;
        bb[i].first = b[i].second;
        bb[i].second = i + 1;
    }
    if(n == 1){
        int h[m];
        for(int i = 0; i < m; i++) {
            h[i] = b[i].first;
            maks += b[i].first;
            br++;
        }
        for(int i = 0; i < m; i++){
            if(maks + b[i].second - b[i].first <= a[0].second) {
                h[i] = b[i].second;
                maks += b[i].second - b[i].first;
            }
            else {
                h[i] += a[0].second - maks;
                maks = a[0].second;
                break;
            }
        }
        cout << maks <<"\n" << br << "\n";
        for(int i = 0; i < br; i++) {
            cout << 1 << " " << i + 1 << " " << h[i] << "\n";
        }
    }
    else if(n == m) {
        int h[m];
        for(int i = 0; i < m; i++) {
                int g = min(b[i].first, a[i].first);
            h[i] = g;
            maks += g;
            br++;
        }
        for(int i = 0; i < m; i++){
            int f = min(b[i].second, a[i].second);
            if(maks + f - h[i] <= f) {
                maks += f - h[i];
                h[i] = f;
            }
            else {
                maks += f - h[i];
                h[i] += f - h[i];
            }
        }
        cout << maks <<"\n" << br << "\n";
        for(int i = 0; i < br; i++) {
            cout << i + 1 << " " << i + 1 << " " << h[i] << "\n";
        }
    }
    else {
        sort(aa, aa + n);
        sort(bb, bb + m);
        for(int i = 0; i < min(n, m); i++){
            maks += min(aa[n - 1 - i].first, bb[m - 1 - i].first);
            one.push_back(aa[n - 1 - i].second);
            two.push_back(bb[m - 1 - i].second);
            three.push_back(min(aa[n - 1 - i].first, bb[m - 1 - i].first));
            br++;
        }
            cout << maks << "\n" << br << "\n";
        for(int i = 0; i < one.size(); i++) {
            cout << one[one.size() - 1 - i] << " " << two[two.size() - 1 - i] << " " << three[three.size() - 1 - i] << "\n";
        }
    }
    return 0;
}