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

#define ll long long
#define ld long double

using namespace std;

ll min(ll a, ll b) { if(a<b) return a; return b; }

const int maxN = 50007;
ll a[maxN], b[maxN], c[maxN], d[maxN];

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

    int n,m;
    cin>>n>>m;

    ll minCol = 0;
    ll maxCol = 0;
    bool a0=true, c0=true;
    for(int i=0;i<n;i++) { cin>>a[i]>>b[i]; if(a[i]!=0) a0=false; }
    for(int i=0;i<m;i++) {
        cin>>c[i]>>d[i];
        if(c[i]!=0)
            c0=false;
        minCol += c[i];
        maxCol += d[i];
    }

    if(n==1) {
        ll posadjeni = min(maxCol, b[0]);
        cout << posadjeni << "\n";
        ll posadjeno[m];
        ll cnt = 0;
        for(int i=0;i<m;i++) {
            posadjeno[i] = c[i];
            posadjeni -= c[i];
            minCol -= c[i];
            if(d[i]-c[i] >= posadjeni-minCol) {
                posadjeno[i] += posadjeni - minCol;
                posadjeni -= posadjeni-minCol;
            }
            if(posadjeno[i] > 0) cnt++;
            if(posadjeni == 0) break;
        }

        cout << cnt << "\n";
        for(int i=0;i<m;i++) {
            if(posadjeno[i] > 0) {
                cout << 1 << " " << i+1 << " " << posadjeno[i] << "\n";
                cnt--;
            }
            if(cnt==0) break;
        }
    } else if(a0 == true && c0 == true) {
        ;
    }


    return 0;
}