#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int n,m,br1,br2,bri1,bri2,sum,c[50005],d[50005],a[50005],b[50005];
void set1(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cout << i << " " << j << " " << c[j] << "\n";
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i=1;i<=n;i++){
        cin >> a[i] >> b[i];
        if (a[i]==b[i]){
            bri1++;
        }
        if (a[i]!=0) br1++;
    }
    for (int i=1;i<=m;i++){
        cin >> c[i] >> d[i];
        if (c[i]==d[i]){
            bri2++;
            sum+=c[i];
        }
        if (c[i]!=0) br2++;
    }
    if (bri1==n and bri2==m){
        cout << sum << "\n" << br1*br2 << "\n";
        set1();
    }
    return 0;
}