#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    long long n, m;
    cin >> n >> m;

    vector<pair<long long,long long>> a(n), b(m);

    long long ma = 0, mb = 0;

    for(long long i = 0; i < n; i++)
    {
        cin >> a[i].first >> a[i].second;
        ma += a[i].second;
    }
    for(long long i = 0; i < m; i++)
    {
        cin >> b[i].first >> b[i].second;
        mb += b[i].second;
    }

    long long maks = min(ma,mb);

    long long br = 0;

    vector<pair<long long,pair<long long,long long>>> r;

    vector<long long> l(n), d(m);

    for(long long i = 0, j = 0; i < n && j < m;)
    {
        long long o = min(a[i].first,b[i].first);
        r.push_back(pair<long long,pair<long long,long long>>(o, pair<long long,long long>(i,j)));
        l[i] = o;
        d[j] = o;
        br += o;
        i += (l[i] == a[i].first);
        j += (d[j] == b[i].first);
    }

    for(long long i = 0, j = 0; i < n && j < m && br < maks;)
    {
        long long o = min(a[i].second - l[i], b[j].second - d[j]);
        r[i+j].first += o;
        l[i] += o;
        d[j] += o;
        i += (l[i] == a[i].second);
        j += (d[j] == b[i].second);
    }

    cout << br << "\n";
    cout << r.size() << "\n";

    for(pair<long long,pair<long long,long long>> i : r)
    {
        cout << i.second.first + 1 << " " << i.second.second + 1 << " " << i.first << "\n";
    }

    return 0;
}