#include <bits/stdc++.h>

using namespace std;
struct trojka{
    int x,y,d;
};

const int MAXN = 5e4;
int a[MAXN],b[MAXN],c[MAXN],d[MAXN];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    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];
    }
    queue<trojka> ispis;
    int k = 0,suma = 0;;
    for(int i  = 0; i<n; i++){
        int j = 0;
        while(b[i]>0&&j<m){
            if(b[i]>=c[j]){
                int x = min(b[i],d[j]);
                a[i]-=x;
                b[i]-=x;
                c[j]-=x;
                d[j]-=x;
                if(x>0){
                    ispis.push({i+1,j+1,x});
                    suma+=x;
                    k++;
                }
            }
            j++;
        }
        if(a[i]>0){
            cout<<0<<endl;
            return 0;
        }
    }
    cout<<suma<<"\n";
    cout<<k<<"\n";
    while(!ispis.empty()){
        trojka t = ispis.front();
        cout<<t.x<<" "<<t.y<<" "<<t.d<<"\n";
        ispis.pop();
    }
    return 0;
}