/// Author: NemanjaSo2005
/// Date: 26th February 2023
/// Contest: Serbian high school regional contest
/// Anyone can use any part of this code for any purpose after the contest.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll N,M;
ll min(ll a,int b){
   if(a<b)
      return a;
   return b;
}
ll min(int a,ll b){
   if(a<b)
      return a;
   return b;
}
ll max(ll a,int b){
   if(a>b)
      return a;
   return b;
}
ll max(int a,ll b){
   if(a>b)
      return a;
   return b;
}
ll R,lbx[50005],ubx[50005],lby[50005],uby[50005],X[50005],Y[50005],sx,sy;
struct slog{
   int x,y;
   ll kol;
}pp;
vector<slog> V;
void stavi(int x,int y,ll kol){
   pp.x=x;
   pp.y=y;
   pp.kol=kol;
   V.push_back(pp);
   return;
}
void solve(){
   cin>>N>>M;
   for(int i=1;i<=N;i++){
      cin>>lbx[i]>>ubx[i];
      sx+=ubx[i];
   }
   for(int i=1;i<=M;i++){
      cin>>lby[i]>>uby[i];
      sy+=uby[i];
   }
   if(sx>sy){
      R=sy;
      for(int i=1;i<=M;i++)
         Y[i]=uby[i];
      ll kx=R;
      for(int i=1;i<=N;i++){
         X[i]=lbx[i];
         kx-=lbx[i];
      }
      for(int i=1;i<=N;i++){
         ll sm=min(kx,ubx[i]-lbx[i]);
         X[i]+=sm;
         kx-=sm;
      }
   }
   else{
      R=sx;
      for(int i=1;i<=N;i++)
         X[i]=ubx[i];
      ll ky=R;
      for(int i=1;i<=M;i++){
         Y[i]=lby[i];
         ky-=lby[i];
      }
      for(int i=1;i<=M;i++){
         ll sm=min(ky,uby[i]-lby[i]);
         Y[i]+=sm;
         ky-=sm;
      }
   }/*
   cout<<"X:"<<endl;
   for(int i=1;i<=N;i++)
      cout<<X[i]<<" ";
   cout<<endl;
   cout<<"Y:"<<endl;
   for(int i=1;i<=M;i++)
      cout<<Y[i]<<" ";
   cout<<endl;*/
   cout<<R<<"\n";
   int pok=1;
   for(int i=1;i<=N;i++){
      while(Y[pok]==0)
         pok++;
      for(int j=pok;j<=M;j++){
         if(X[i]==0)
            break;
         if(Y[j]==0)
            continue;
         ll k;
         k=min(Y[j],X[i]);
         stavi(i,j,k);
         Y[j]-=k;
         X[i]-=k;
      }
   }
   cout<<V.size()<<"\n";
   for(int i=0;i<V.size();i++)
      cout<<V[i].x<<" "<<V[i].y<<" "<<V[i].kol<<"\n";
   return;
}
void reset(){}
int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   int T=1;
   //cin>>T;
   while(T--){
      solve();
      reset();
   }
   return 0;
}