#include<bits/stdc++.h>
using namespace std;
int n,m,maks,pocetaki[10005],brp,pocetakj[10005],suma,kraji[10005],krajj[10005],bri[10005],brj[10005];
int mp[5000][5000];
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>pocetaki[i]>>kraji[i];
    }
    for(int i=1;i<=m;i++){
        cin>>pocetakj[i]>>krajj[i];
    }
    for(int i=1;i<=n;i++){
        suma+=kraji[i];
    }
    while(maks!=suma){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if((bri[i]<kraji[i]) and (brj[j]<krajj[j])){
                mp[i][j]++;
                bri[i]++;
                brj[j]++;
                maks++;
        }
        }
        }
    }
        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(brj[j]<pocetakj[j]){
                mp[i][j-1]--;
                mp[i][j]++;
                brj[j]++;
            }
        }
        }
        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
           if(mp[i][j]!=0)
            brp++;
        }
        }
        cout<<suma<<endl;
        cout<<brp<<endl;

        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(mp[i][j]!=0){
                cout<<i<<" "<<j<<" "<<mp[i][j]<<"\n";
            }
        }
        }
return 0;
}