#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;


struct polje{
    ll maxel;
    int x, y;
};

bool sp(polje a, polje b){
    return (a.maxel > b.maxel);
}

int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll m, n, i, j, br=0, k;
    cin >> n >> m;
    vector <pair<ll, ll> > a(n);
    vector <pair<ll, ll> > b(m);

    for(i=0;i<n;i++){
        ll x, y;
        cin >> x >> y;
        a[i] = make_pair(x, y);
    }
    for(i=0;i<m;i++){
        ll x, y;
        cin >> x >> y;
        b[i] = make_pair(x, y);
    }
    vector<polje> el;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            polje p;
            p.maxel = min(a[i].second, b[j].second);
            p.x = i;
            p.y = j;
            if(p.maxel != 0){
                el.push_back(p);
                br++;
            }
            /*if(maxe < d[i][j].first){
                maxe = d[i][j].first;
                maxi = i;
            }
            if(mine < d[i][j].second){
                maine = d[i][j].second;
                mini = j;
            }*/
        }
    }
    sort(el.begin(), el.end(), sp);

    /*for(i=0;i<br;i++){
        cout << el[i].maxel << " " << el[i].x << " " << el[i].y << endl;
    }*/

    for(i=0;i<br-1;i++){
        for(j=i+1;j<br;j++){
            if(el[i].x == el[j].x || el[i].y == el[j].y){
                el.erase(el.begin()+j);
                br--;
                j--;
                /*cout << endl;
                for(k=0;k<br;k++){
                    cout << el[k].maxel << " " << el[k].x << " " << el[k].y << endl;
                }*/
            }
        }
    }

    ll sum = 0;
    for(i=0;i<br;i++){
        sum += el[i].maxel;
    }
    cout << sum << endl << br << endl;
    for(k=0;k<br;k++){
        cout << el[k].maxel << " " << el[k].x << " " << el[k].y << endl;
    }
    return 0;
}

/*
3 3
2 4
1 3
0 1
1 3
1 1
0 4
*/