#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N,M,br=0,K=0;
    cin >> N >> M;
    vector<pair<int,int>> ab(N);
    vector<pair<int,int>> cd(M);
    for(int i=0; i<N; i++){
        cin >> ab[i].first >> ab[i].second;
    }
    for(int i=0; i<M; i++){
        cin >> cd[i].first >> cd[i].second;
    }

    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            if(ab[i].second<cd[j].second && ab[i].second != 0 && cd[j].second != 0){
                br += ab[i].second;
                K++;
            }
            else if(ab[i].second>cd[j].second && ab[i].second != 0 && cd[j].second != 0){
                br += cd[j].second;
                K++;
            }
        }
    }
    cout << br <<"\n";
    cout << K <<"\n";

    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            if(ab[i].second != 0 && cd[j].second != 0){
                cout << i+1 << " " << j+1 << " ";
                if(ab[i].second<cd[j].second){
                    cout << ab[i].second <<"\n";
                }
                else{
                    cout << cd[i].second <<"\n";
                }
            }
        }
    }

    return 0;
}