#include <bits/stdc++.h>

using namespace std;

const int MAXN = 50010;
long long a[MAXN];
long long b[MAXN];
long long c[MAXN];
long long d[MAXN];


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

#ifdef OKRUZNOMAKS
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w",stdout);
#endif // OKRUZNOMAKS

    map<pair<int, int>, long long> ans;

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i] >> b[i];
    }
    for (int i = 0; i < m; i++)
    {
        cin >> c[i] >> d[i];
    }

    int j = 0;
    for(int i = 0; i < n; i++)
    {
        for (; j < m; j++)
        {
            if (b[i] >= c[i] && b[i] <= d[i])
            {
                int x = b[i];
                b[i] -= x;
                d[j] -= x;
                krumpiri += (long long)x;
                if(x != 0)
                    ans[{i,j}]+=x;
                break;
            }
            int x = min(a[i], c[j]);
            a[i] -= x;
            b[i] -= x;
            c[j] -= x;
            d[j] -= x;
            krumpiri += (long long)x;
            if(x != 0)
                ans[ {i,j}]+=x;
            if (a[i] == 0)
                break;
        }
    }
    j = 0;
    for(int i = 0; i < n; i++)
    {
        for (; j < m; j++)
        {
            if (b[i] == 0)
                break;
            int x = min(b[i], d[j]);
            b[i] -= x;
            d[j] -= x;
            if(x != 0)
                ans[ {i,j}]+=x;
            krumpiri += (long long)x;
            if (b[i] == 0)
                break;
        }
    }
    cout << krumpiri << "\n";
    cout << ans.size() << "\n";
    for(auto g: ans)
    {
        cout <<g.first.first+1 << " " << g.first.second+1<< " "  <<g.second << "\n";
    }



    return 0;
}