#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);cin.tie(0);
    int N, M;
    cin >> N >> M;
    pair<int, int> redovi[N], kolone[M];
    
    int sumn=0, summ=0;

    for(int i = 0; i < N; i++) {
        cin >> redovi[i].first >> redovi[i].second;
        sumn += redovi[i].second;
    }
    for(int i = 0; i < M; i++) {
        cin >> kolone[i].first >> kolone[i].second;
        summ += kolone[i].second;
    }
    int max = summ>sumn?sumn:summ;
    vector<int> x, y, n;
    if(summ >= sumn) {
        int kolonepom[M]{},redovipom[M]{};
        for(int i = 0; i < N; i++) {
            int nn[M]{};
            redovipom[i] = redovi[i].second;
            while(redovipom[i] > 0) {
                int prom = 0;
                for(int j = 0; j<M; j++){ 
                    if(redovipom[i] == 0)goto label2;
                    if(kolonepom[j] < kolone[j].first) {
                        kolonepom[j]++;nn[j]++;
                        redovipom[i]--;
                        prom++;
                    }
                }
                if(prom == 0) goto label1;
            }
            label1:
            while(redovipom[i] > 0) {
                for(int j = 0; j<M; j++){ 
                    if(redovipom[i] == 0)goto label2;
                    if(kolonepom[j] < kolone[j].second) {
                        kolonepom[j]++;nn[j]++;
                        redovipom[i]--;
                    }
                }
            }
            label2:
            for(int j = 0; j < M; j++) {
                if(nn[j] != 0) {
                    x.push_back(i);
                    y.push_back(j);
                    n.push_back(nn[j]);
                }
            }
        }
    }else{
        int kolonepom[M]{},redovipom[M]{};
        for(int i = 0; i < M; i++) {
            kolonepom[i] = kolone[i].second;
            int nn[N]{};
            while(kolonepom[i] > 0) {
                int prom = 0;
                for(int j = 0; j<N; j++){ 
                    if(kolonepom[i] == 0)goto label4;
                    if(redovipom[j] < redovi[j].first) {
                        redovipom[j]++;nn[j]++;
                        kolonepom[i]--;
                        prom++;
                    }
                }
                if(prom == 0) goto label3;
            }
            label3:
            while(kolonepom[i] > 0) {
                for(int j = 0; j<N; j++){ 
                    if(kolonepom[i] == 0)goto label4;
                    if(redovipom[j] < redovi[j].second) {
                        redovipom[j]++;nn[j]++;
                        kolonepom[i]--;
                    }
                }
            }
            label4:
            for(int j = 0; j < N; j++) {
                if(n[j] != 0) {
                    x.push_back(j);
                    y.push_back(i);
                    n.push_back(nn[j]);
                }
            }
        }
    }
    cout << max << "\n";
    
    cout << x.size() << "\n";
    for(int i = 0; i < x.size(); i++) {
        cout << x[i]+1 << " " << y[i]+1 << " " << n[i] << "\n";
    }
    return 0;
}