#include <iostream>
#include <vector>
using namespace std;

int main()
{
    cin.tie(0);
    int n;
    int m;
    cin>>n;
    cin>>m;
    int brojacR[n];
    int brojacK[m];
    int kupus = 0;
    int polja = 0;
    int a[n];
    int b[n];
    int c[m];
    int d[m];
    int matrica[n][m];
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i];
        brojacR[i]=0;
    }
    for(int i=0; i<m; i++){
        cin>>c[i]>>d[i];
        brojacK[i] =0;
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            matrica[i][j]=0;
        }
    }
    while(true){
              int g = kupus;
    for(int i=0; i<n; i++){

        for(int j= 0; j<m; j++){
            if(brojacR[i]<b[i] && brojacK[j]<d[j]){
                if(matrica[i][j]==0){
                    polja++;
                }
                kupus++;
                matrica[i][j]++;
                brojacK[j]++;
                brojacR[i]++;

            }
        }

    }
      if(g==kupus){
        break;
    }
    }
    cout<<kupus<<endl;
    cout<<polja<<endl;

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(matrica[i][j]!=0){
                cout<<i+1<<" "<<j+1<<" "<<matrica[i][j]<<endl;
            }
        }
    }

    return 0;
}