#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    vector<vector<ll>> c(n,vector<ll>(m));
    vector<pair<ll,ll>> a(n),b(m);
    for(ll i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    for(ll i=0;i<m;i++){
        cin>>b[i].first>>b[i].second;
    }
    ll p=0,q=0;
    for(ll i=0;i<m;i++){
        p+=b[i].second;
    }
    if(p<a[0].second){
        cout<<p<<'\n';
        for(ll i=0;i<m;i++){
            cout<<1<<" "<<i+1<<" "<<b[i].second<<'\n';
        }
    }
    else{
        cout<<a[0].second<<'\n';
        for(ll i=0;i<m;i++){
            c[0][i]=b[i].first;
            q+=c[0][i];
        }
        for(ll i=0;i<m;i++){
            if(q>=a[0].second){
                cout<<1<<" "<<i+1<<" "<<b[i].first<<'\n';
            }
            if(q<a[0].second){
                if(q+b[i].second-b[i].first<a[0].second){
                    c[0][i]=b[i].second;
                    q+=b[i].second-b[i].first;
                    cout<<1<<" "<<i+1<<" "<<b[i].second<<'\n';
                }
                else{
                    c[0][i]=c[0][i]+a[0].second-q;
                    q=a[i].second;
                    cout<<1<<" "<<i+1<<" "<<c[0][i]<<'\n';
                }

            }
        }
    }

}