#include <bits/stdc++.h>
#define MAXN 300010
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>

using namespace std;

void solve()
{
    bool t1 = true, t2 = true;
    int n, m;
    cin >> n >> m;
    ll res = 0;
    vector<int> a(n+1),  b(n+1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i] >> b[i];
        if (a[i] != b[i])
            t2 = false;
        res += b[i];
    }
    vector<int> c(m+1), d(m+1), psumc(m+1);
    psumc[0] = 0;
    for (int i = 1; i <= m; i++)
    {
        cin >> c[i] >> d[i];
        psumc[i] = psumc[i-1] + c[i];
        if (!(a[i] <= c[i] && c[i] <= b[i] && b[i] <= d[i]))
            t1 = false;
        if (c[i] != d[i])
            t2 = false;
    }
    int k = 0;
    if (n == m && t1)
    {
        k = n;
        cout << res << '\n' << k << '\n';
        for (int i = 1; i <= n; i++)
        {
            cout << i << ' ' << i << ' ' << b[i] << '\n';
        }
        return;
    }
    if (t2)
    {
        cout << res << '\n';
        stack<pair<pii, int>> ans;
        int i = 1, j = 1;
        while (i <= n && j <= m)
        {
            if (a[i] < c[j])
            {
                k++;
                ans.push(mp(mp(i, j), a[i]));
                c[j] -= a[i];
                i++;
            }
            else if (a[i] > c[j])
            {
                k++;
                ans.push(mp(mp(i, j), c[j]));
                a[i] -= c[j];
                j++;
            }
            else
            {
                k++;
                ans.push(mp(mp(i, j), c[j]));
                i++; j++;
            }
        }
        cout << k << '\n';
        while (!ans.empty())
        {
            cout << ans.top().fi.fi << ' ' << ans.top().fi.se << ' ' << ans.top().se << '\n';
            ans.pop();
        }
        return;
    }
    if (n == 1)
    {
        stack<pair<pii, int>> ans;
        res = 0;
        for (int i = m; i >= 1; i--)
        {
            if (d[i] == 0) continue;
            if (d[i] < b[1] - psumc[i-1])
            {
                k++;
                res += d[i];
                ans.push(mp(mp(1,i), d[i]));
                b[1] -= d[i];
            }
            else
            {
                k++;
                res += b[1] - psumc[i-1];
                ans.push(mp(mp(1,i), b[1] - psumc[i-1]));
                b[1] -= b[1] - psumc[i-1];
            }
        }
        cout << res << '\n' << k << '\n';
        while (!ans.empty())
        {
            cout << ans.top().fi.fi << ' ' << ans.top().fi.se << ' ' << ans.top().se << '\n';
            ans.pop();
        }
        return;
    }
    cout << k << '\n';
}

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

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