#include <bits/stdc++.h>

using namespace std;



int main()
{

    int n,m;
    cin>>n>>m;
int potato[n][m];
    int a,b,c,d;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        for(int j=1;j<=m;j++)
          potato[i][j]=b;

    }

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

    for(int i=1;i<=n;i++){


        for(int j=1;j<=m;j++){
         if(potato[i][j]>0){
            sum+=potato[i][j];
            br++;

            cout<<i<<" "<<j<<" "<<potato[i][j]<<endl;
         }
        }
    }
    cout<<sum<<endl;
    cout<<br<<endl;




    return 0;
}