#include <iostream>

using namespace std;
typedef struct{
    int a;
    int b;
}pom;
int main(){
    int n,m,brk=0,brp=0,as=0;
    cin>>n>>m;
    //int krn[n], krm[m];
    int polje[n][m];
    pom ogrn[n];
    pom ogrm[m];
    pom koord[n*m];
    for(int i=1; i<=n; i++){
        cin>>ogrn[i].a;
        cin>>ogrn[i].b;
        //krn[i]=ogrn[i].b;
    }
    for(int i=1; i<=m; i++){
        cin>>ogrm[i].a;
        cin>>ogrm[i].b;
        //krm[i]=ogrm[i].b;
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(ogrn[i].b<ogrm[j].b){
                polje[i][j]=ogrn[i].b;
                brk=brk+polje[i][j];
                if(polje[i][j]!=0){
                    brp++;
                    koord[as].a=i;
                    koord[as].b=j;
                    as++;
                }
            }
            else{
                polje[i][j]=ogrm[j].b;
                brk=brk+polje[i][j];
                if(polje[i][j]!=0){
                    brp++;
                    koord[as].a=i;
                    koord[as].b=j;
                    as++;
                }
            }
        }
    }
    cout<<brk<<endl<<brp<<endl;
    for(int i=0;i<as;i++){
        cout<<koord[i].a<<" ";
        cout<<koord[i].b<<" ";
        cout<<polje[koord[i].a][koord[i].b]<<endl;
    }
    return 0;
}