#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);cin.tie(0);
    int N, M;
    cin >> N >> M;
    int matrica[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;
    if(summ >= sumn) {
        int kolonepom[M]{},redovipom[M]{};
        for(int i = 0; i < N; i++) {
            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]++;
                        redovipom[i]--;
                        matrica[i][j]++;
                        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]++;
                        redovipom[i]--;
                        matrica[i][j]++;
                    }
                }
            }
            label2:continue;
        }
    }else{
        int kolonepom[M]{},redovipom[M]{};
        for(int i = 0; i < M; i++) {
            kolonepom[i] = kolone[i].second;
            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]++;
                        kolonepom[i]--;
                        matrica[j][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].first) {
                        redovipom[j]++;
                        kolonepom[i]--;
                        matrica[j][i]++;
                    }
                }
            }
            label4:continue;
        }
    }
    cout << max << "\n";
    int k = 0;
    for(int i = 0; i < N; i++) 
        for(int j = 0; j < M; j++)
            if(matrica[i][j] != 0)k++;
    
    cout << k << "\n";
    for(int i = 0; i < N; i++) 
        for(int j = 0; j < M; j++)
            if(matrica[i][j] != 0) cout << i+1 << " " << j+1 << " " << matrica[i][j] << "\n";
    return 0;
}