#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define ld long double
#define llinf 100000000000000000LL
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define pll pair<ll,ll>
#define endl '\n'
#define all(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define ios ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
//using namespace __gnu_pbds;
using namespace std;
//template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int MOD=1000000007;
const int mxN=5e4+5;
ll a[mxN],b[mxN],c[mxN],d[mxN];
void solve(){
	ll n, m;
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		cin >> a[i] >> b[i];
	}
	for(int i=1;i<=m;i++){
		cin >> c[i] >> d[i];
	}
    ll ans=0,k=0;
    vector<pair<ll,ll>> poz;
    vector<ll> kolko;
    int i=0,j=0;
    for(i=1;i<=n;){
        for(j=1;j<=m;){
            ans+=min(b[i],d[j]);
            if(min(b[i],d[j])!=0){
                k++;
                poz.pb({i,j});
                kolko.pb(min(b[i],d[j]));
            }
            if(min(b[i],d[j])==b[i]){
                i++;
                j=1;
                break;
            }
            else{
                j++;
                i=1;
                break;
            }
        }
    }
    cout << ans << endl;
    cout << k << endl;
    for(int i=0;i<sz(kolko);i++){
        cout << poz[i].fi << ' ' << poz[i].sc << ' ' << kolko[i] << endl;
    }
}

int main(){
    ios;
	int t=1;
	//cin >> t;
	while(t--){
       solve();
	}
    return 0;
}