#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin>>n>>m;
    if(n==1){
        ll a, b;
        cin>>a>>b;
        vector<ll> c(m), d(m);
        for(int i=0; i<m; i++) cin>>c[i]>>d[i];
        vector<ll> v(m, 0);
        for(int i=0; i<m; i++) v[i]=c[i];
        ll suma=accumulate(all(v), 0ll);
        for(int i=0; i<m; i++){
            if(suma+d[i]-c[i]<=b){
                v[i]=d[i];
                suma=suma+d[i]-c[i];
            }
            else{
                v[i]=v[i]+b-suma;
		        suma=b;
                break;
            }
        }
        cout<<suma<<'\n';
        ll k=0;
        for(int i=0; i<m; i++){
            if(v[i]) k++;
        }
        cout<<k<<'\n';
        for(int i=0; i<m; i++){
               // cout<<v[i]<<' ';
            if(v[i]){
                cout<<"1 "<<i+1<<' '<<v[i]<<'\n';
            }
        }
    }
    else{

    vector<ll> a(n), b(n), c(m), d(m)/*, ooo(m, 0)*/, ii, jj, mm;
    for(int i=0; i<n; i++) cin>>a[i]>>b[i];
    for(int i=0; i<m; i++) cin>>c[i]>>d[i];
    //vector< vector<int> > ans(n, ooo);
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int m=min(a[i], c[j]);
            //ans[i][j]=m;
            a[i]-=m;
            c[j]-=m;
            if(m){
                ii.push_back(i+1);
                jj.push_back(j+1);
                mm.push_back(m);
            }
            if(a[i]==0) break;
        }
    }
    cout<<accumulate(all(mm), 0ll)<<'\n';
    cout<<mm.size()<<'\n';
    for(int i=0; i<mm.size(); i++) cout<<ii[i]<<' '<<jj[i]<<' '<<mm[i]<<'\n';


    }
    return 0;
}