#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

const ll LINF = 4e18;
const int mxN = 1e6+10, INF = 2e9;
ll n, m;
vector<ll> a, b, c, d, v;
map<array<int, 2>, ll> ans;
bool swapped;

void Add(int i, int j, ll s){

    if (!s) return;
    if (swapped) swap(i, j);
    i++; j++;
    ans[{i, j}] += s;
}

void Swap(){

    swap(n, m);
    swap(a, c);
    swap(b, d);
    swapped = 1;
}

void Solve(){

    cin >> n >> m;
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];
    c.resize(m);
    d.resize(m);
    for (int i = 0; i < m; i++)
        cin >> c[i] >> d[i];
    array<ll, 2> sum = {0, 0};
    for (int i = 0; i < n; i++)
        sum[0] += b[i];
    for (int i = 0; i < m; i++)
        sum[1] += d[i];
    if (sum[0] > sum[1]) Swap();
    v.assign(m, 0);
    for (int i = 0; i < m; i++)
        v[i] = c[i];
    for (int i = 0, j = 0; i < n; i++){
        ll cur = 0;
        while (1){
            if (j == m){
                for (int k = 0; k < m; k++)
                    v[k] = d[k] - c[k];
                j = 0;
            }
            ll s = min(b[i] - cur, v[j]);
            Add(i, j, s);
            cur += s; v[j] -= s;
            if (cur == b[i]) break;
            j++;
        }
    }
    cout << sum[0] << en;
    cout << ans.size() << en;
    for (auto x : ans)
        cout << x.first[0] << sp << x.first[1] << sp << x.second << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}