#include <bits/stdc++.h>

using namespace std;
struct polje{
    int x;
    int y;
    int vrednost;
}p;
const int MAXMN=5e4;
vector<polje>mars;
int N,M;
vector<int>a(MAXMN+5);
vector<int>b(MAXMN+5);
vector<int>c(MAXMN+5);
vector<int>d(MAXMN+5);
vector<int>zbirPoX(MAXMN+5,0);
vector<int>zbirPoY(MAXMN+5,0);
int prebroj0(){
    int br=0;
    for(auto i:mars){
        if(i.vrednost==0){
            br++;
        }
    }
    return br;
}
void ispisiMars(){

    cout<<mars.size()-prebroj0();
    for(auto i:mars){
        cout<<i.x<<" "<<i.y<<" "<<i.vrednost<<"\n";
    }
}
void ispisiMarsN1(){
    cout<<zbirPoX[0]<<"\n";
    cout<<mars.size()-prebroj0()<<"\n";
    for(auto i:mars){
        cout<<i.x<<" "<<i.y<<" "<<i.vrednost<<"\n";
    }
}
void rasporedi(){
    int i=0,j=0;
    //for(int i=0;)
}
void zaN1(){
    for(int i=0;i<M;i++){
        p.x=0;
        p.y=i;
        p.vrednost=d[i];
        mars.push_back(p);
        zbirPoX[0]+=d[i];
    }
    if(zbirPoX[0]<=b[0]){
        ispisiMarsN1();
        return;
    }
    for(int i=0;i<mars.size();i++){
        mars[i].vrednost=c[mars[i].y];
        zbirPoX[0]+=(c[mars[i].y]-d[mars[i].y]);
        if(zbirPoX[0]<=b[0]){
            mars[i].vrednost+=(b[0]-zbirPoX[0]);
            ispisiMarsN1();
            break;
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>N>>M;
    for(int i=0;i<N;i++){
        cin>>a[i];
        cin>>b[i];
        /*p.x=i;
        p.y=0;
        p.vrednost=b[i];
        mars.push_back(p);
        zbirPoX[i]=b[i];*/
    }
    for(int i=0;i<M;i++){
        cin>>c[i];
        cin>>d[i];
    }
    if(N==1){
        zaN1();
        return 0;
    }
    return 0;
}