#include <bits/stdc++.h>

using namespace std;
struct trojka{
    int x,y,d;
};
struct isecak{
    int ind,first,second;
};
bool cmp(isecak t, isecak l){
    if(t.second == l.second){
        return t.first<l.first;
    }
    return t.second>l.second;
}
const int MAXN = 5e4;
isecak red[MAXN],kolona[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>>red[i].first>>red[i].second;
        red[i].ind = i;
    }
    for(int i = 0; i<m; i++){
        cin>>kolona[i].first>>kolona[i].second;
        kolona[i].ind = i;
    }
    sort(red,red+n,cmp);
    sort(kolona,kolona+m, cmp);
    queue<trojka> ispis;
    int k = 0,suma = 0;
    for(int i  = 0; i<n; i++){
        int j = 0;
        while(red[i].second>0&&j<m){
            if(red[i].second>=kolona[j].first){
                int x = min(red[i].second,kolona[j].second);
                red[i].first-=x;
                red[i].second-=x;
                kolona[j].first-=x;
                kolona[j].second-=x;
                if(x>0){
                    ispis.push({red[i].ind+1,kolona[j].ind+1,x});
                    suma+=x;
                    k++;
                }
            }
            j++;
        }
        if(red[i].first>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;
}