#include <bits/stdc++.h>

using namespace std;

const int N=50000;


int a,b[N],c,d[N],x[N],y[N],k[N];
int main()
{

    int n,m;
    cin>>n>>m;


    for(int i=1;i<=n;i++){
        cin>>a>>b[i];
    }
    for(int i=1;i<=m;i++){
         cin>>c>>d[i];
    }
    int sum=0,br=0;
    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++){
           if(min(b[i],d[j])>0){

            br++;
            x[br]=i;
            y[br]=j;
            k[br]=min(b[i],d[j]);
            sum+=min(b[i],d[j]);
        }
        }
    }

 cout<<sum<<endl;
 cout<<br<<endl;


for(int i=1;i<=br;i++){
    cout<<x[i]<<" "<<y[i]<<" "<<k[i]<<endl;
}






    return 0;
}