#include <bits/stdc++.h>
using namespace std;
long long n,m,mat[3][50005],rn,rm,kn,km,maxkol,brkromp,brpolja,niz[50005],br,dodajjos,suma;
struct slog{
    int poz,maxx,minn;
}kolone[50005];
bool cmp (slog a, slog b){
    return a.maxx>b.maxx;
}
int main (){
    maxkol=0;
    cin>>n>>m;
    for(int i=1; i<=n; i++){
        cin>>rn>>rm;
    }
    for(int i=1; i<=m;i++){
        cin>>kn>>km;
        suma+=km;
        kolone[i].maxx=km;
        kolone[i].minn=kn;
        kolone[i].poz=i;
        mat[1][i]=kn;
        brkromp+=kn;
        if(kn>0)
        brpolja+=1;
    }
    if(n==1 and m==1){
        cout<<min(rm,km)<<endl;
        cout<<1<<endl;
        cout<<1<<" "<<1<<" "<<min(rm,km);
        return 0;

    }

       sort(kolone+1,kolone+m+1,cmp);
       br=0;
       dodajjos=min(rm,suma)-brkromp;
            while(br<dodajjos){
                    for(int i=1; i<=m; i++){
                            if( mat[1][kolone[i].poz]==kolone[i].maxx)continue;
                            else{
                                if(mat[1][kolone[i].poz]==0)brpolja++;
                    if(kolone[i].maxx-mat[1][kolone[i].poz]>dodajjos){
        mat[1][kolone[i].poz]+=dodajjos;
        br=dodajjos;
        break;
                    }
                    else{
                            br+=kolone[i].maxx-mat[1][kolone[i].poz];
                         mat[1][kolone[i].poz]=kolone[i].maxx;
                    }
                            }
      // if(br==km-brkromp)break;
       }
            }
            /*
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cout<<mat[i][j]<<" ";
        }
        cout<<endl;
    }
    */
    /*
    for(int i=1; i<=m; i++)
     cout<<kolone[i].maxx<<" "<<kolone[i].poz<<endl;
     */
     cout<<min(rm,suma)<<endl;
     cout<<brpolja<<endl;
     for(int j=1; j<=m; j++){
         if(mat[1][j]>0)cout<<1<<" "<<j<<" "<<mat[1][j]<<endl;
     }
return 0;
}
/*
1 5
1 10
3 5
4 5
1 2
0 7
0 0
*/
/*
1 5
1 10
3 5
4 5
1 2
0 1
0 5
*/