#include <bits/stdc++.h>
#define FAST {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define ll long long
#define ull unsigned long long
#define pqueue priority_queue
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) (a).begin(), (a).end()
#define mp make_pair
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
*/
// order_of_key -> # less than k
// find_by_order -> k-th element
// pq max element

const int NMAX=50100;

ll iski[NMAX];
ll iskj[NMAX];
int main () {
    FAST
    int n , m;
    cin >> n >> m;
    vector<pii>v1(n);
    vector<pii>v2(m);
    ll s1 = 0;
    ll s2 = 0;
    for (int i = 0 ; i < n ; i++ ) {
        cin >> v1[i].se >> v1[i].fi;
        s1 += v1[i].fi;
    }
    for (int i = 0 ; i < m ; i++ ) {
        cin >> v2[i].se >> v2[i].fi;
        s2 += v2[i].fi;
    }

    for(int i = 0 ; i < n ; i++ ) {
        swap(v1[i].fi, v1[i].se);
    }
    for(int i = 0 ; i < m ; i++ ) {
        swap(v2[i].fi, v2[i].se);
    }
    bool swapic = false;
    if ( s2 < s1 ) {
        swap(n,m);
        swap(v1,v2);
        swapic=true;
        swap(s1,s2);
    }

    //for(auto u : v1 ) cout << '#' << u.fi << ' ' << u.se << endl;

    int i = 0;
    int j = 0;
    ll cnt = 0;
    map<pll,ll>M;
    while(i < n && j < m && cnt < s1) {
        ll temp = v1[i].se - iski[i];
        if ( temp == 0 ) {
            i++;
            continue;
        }
        //cout << temp << "!" << endl;
        temp = min(temp, v2[j].fi - iskj[j]);
        if ( temp == 0 ) {
            j++;
            continue;
        }
        cnt += temp;
        M[{i,j}] += temp;
        iski[i] += temp;
        iskj[j] += temp;
    }
    //*
    i= j = 0;
    while(i < n && j < m && cnt < s1) {
        ll temp = min(s1-cnt, v1[i].se - iski[i]);
        if (temp == 0) {
            i++;
            continue;
        }
        //cout << temp << "!" << endl;
        temp = min(temp, v2[j].se - iskj[j]);
        if ( temp == 0 ) {
            j++;
            continue;
        }
        cnt += temp;
        M[{i,j}] += temp;
        iski[i] += temp;
        iskj[j] += temp;
    }
    //*/
    vector<vector<int>>res;
    for (auto u : M) {
        if (u.se) {
            res.pb({u.fi.fi, u.fi.se, u.se});
        }
    }
    if (swapic) {
        for (auto &u : res) {
            swap(u[0], u[1]);
        }
    }

    cout << s1 << endl;
    cout << res.size() << endl;
    for (auto u : res) {
        for (auto uu : u ) {
            cout << uu << ' ';
        }
        cout << endl;
    }



}