#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> vrste(n);
    vector<pair<int, int>> kolone(m);
    for(int i = 0; i < n; i++) {
        cin >> vrste[i].first >> vrste[i].second;
    }
    for(int i = 0; i < m; i++) {
        cin >> kolone[i].first >> kolone[i].second;
    }
    if(n == 1) {
        int red[m];
        ll sum = 0;
        for(int i = 0; i < m; i++) {
            red[i] = kolone[i].first;
            sum += kolone[i].first;
        }
        ll remaining = vrste[0].second - sum;
        int index = 0;
        while(remaining > 0 && index < m) {
            ll delta = min(remaining, (ll)kolone[index].second - red[index]);
            remaining = remaining - delta;
            red[index] += delta;
            index++;
        }
        sum = 0;
        int k = 0;
        for(int i = 0; i < m; i++) {
            sum += red[i];
            if(red[i] > 0) {
                k++;
            }
        }
        cout << sum << "\n";
        cout << k << "\n";
        for(int i = 0; i < m; i++) {
            if(red[i] > 0) {
                cout << 1 << " " << i + 1 << " " << red[i] << "\n";
            }
        }
    } else if(n == m){
        ll max_sum = 0;
        for(int i = 0; i < n; i++) {
            max_sum += min(vrste[i].second, kolone[i].second);
        }
        cout << max_sum << "\n";
        cout << n << "\n";
        for(int i = 0; i < n; i++) {
            cout << i + 1 << " " << i + 1 << " " << min(vrste[i].second, kolone[i].second) << "\n";
        }
    }
    return 0;
}