#include <iostream>

using namespace std;

int main()
{
    int n, m;
    cin>>n>>m;
    int a[n], b[n], c[m], d[m]; 
    
    for(int i=0; i<n; i++)
    {
        cin>>a[i]>>b[i];
    }
    for(int i=0; i<n; i++)
    {
        cin>>c[i]>>d[i];
    }
    
    int brk = 0;
    int poz = 0;
    int k[n*m*3];
    int kbr =0;
    
    for(int i =0; i<n; i++)
    {
        for(int j =0; j<m; j++)
        {
            if(a[i]==0 || c[j] == 0)
            {
                continue;
            }
            if(b[i]>d[j])
            {
                brk = brk + d[j];
                poz++;
                k[kbr] = i+1;
                k[kbr+1] = j+1;
                k[kbr+2] = d[j];
                kbr = kbr+3;
            }else
            {
                brk = brk + b[i];
                poz++;
                k[kbr] = i+1;
                k[kbr+1] = j+1;
                k[kbr+2] = b[i];
                kbr = kbr+3;
            }
        }
    }
    
    cout<<brk<<endl;
    cout<<poz<<endl;
    for(int i =0; i<kbr; i=i+3)
    {
        cout<<k[i]<<" "<<k[i+1]<<" "<<k[i+2]<<endl;
    }
    
    return 0;
}