#include <bits/stdc++.h>
using namespace std;
long long n,m,a[50005],b[50005],c[50005],d[50005],sol[50005],temp,k,na,nb;
struct slog{
  long long x,y,v;
}resenja[7000005];
int main(){
  scanf("%lld%lld",&n,&m);
  if(n==1){
    k=m;
    scanf("%lld%lld",&na,&nb);
    for(int i=1; i<=m; i++){
      scanf("%lld%lld",&a[i],&b[i]);
      if(b[i]==0)k--;
      sol[i]=a[i];
      temp+=a[i];
    }
    for(int i=1; i<=m; i++){
       if(nb-temp>b[i]-a[i]){
          sol[i]=b[i];
          temp+=b[i]-a[i];
       }
       else{
        sol[i]+=nb-temp;
        temp=nb;
        break;
       }
    }
    printf("%lld ",temp);
    printf("%lld \n",k);
    for(int i=1; i<=m; i++)
      printf("1 %d %lld\n",i,sol[i]);
  }
  else{
    int counter=1;
    k=0;
    for(int i=1; i<=n; i++){
      scanf("%lld%lld",&a[i],&b[i]);
    }
    for(int i=1; i<=m; i++){
      scanf("%lld%lld",&c[i],&d[i]);
    }
    int s=1;
    for(int i=1; i<=n; i++){
      for(int j=s; j<=m; j++){
        //cout << i << " " << j << endl;
        if(b[i]==0){
          break;
        }
        if(d[j]==0){
          s++;
          continue;
        }
        else{
          k++;
          int kolicina=min(b[i],d[j]);
          resenja[counter].x=i;
          resenja[counter].y=j;
          resenja[counter].v=kolicina;
          b[i]-=kolicina;
          d[j]-=kolicina;
          temp+=kolicina;
          counter++;
        }
      }
    }
    printf("%lld\n",temp);
    printf("%lld\n",k);
    for(int i=1; i<counter; i++){
      printf("%lld %lld %lld\n",resenja[i].x,resenja[i].y,resenja[i].v);
    }
  }
  return 0;
}