#include <bits/stdc++.h>
# define endl            '\n'
# define clr(x,a)        memset(x,a,sizeof(x))
# define vi              vector<int>
# define all(a)          a.begin(), a.end()
# define deb(x)          cout<<#x<<"=="<<x<<endl;
# define pb              push_back
# define sc              scanf
# define pr              printf
# define tc              int t; cin>>t; while(t--)
# define rev(s)          reverse(s.begin(),s.end())
# define linija          cout<<"___________\n";
using namespace std;
typedef long long ll;
const int mxN=50005, mxM=200005, LOG=25, inf=1e8;
const ll mod=998244353;
template<typename T> T nzd(T a, T b){if(b==0) return a;else return nzd(b, a%b);}
template<typename T> T nzs(T a, T b){return(a*(b/nzd(a,b)));}
template<typename T> T stepenuj(T e, T n){T x=1,p=e;while(n){if(n&1)x=(x*p)%mod;p=(p*p)%mod;n>>=1;}return x;}
template<typename T> inline T na2(T x){return x*x;}
ll n, m, a[mxN], b[mxN], c[mxN], d[mxN], br, v;
ll suma;
struct polje{
   ll r, c, n;
}niz[200006];

struct par{
   ll l, r, id;
}kol[mxN], red[mxN];

struct slog{
   ll id ,val;
   bool operator < (const slog &a) const{
      return val > a.val; /// znak vece je zbog max heapa u C++
   }
}pom, pom1, pom2;

priority_queue<slog> redovi, kolone;

bool cmp(polje a, polje b){
   return ((a.r < b.r) ||
           (a.r==b.r && a.c < b.c) );
}

int main(){
//   freopen("in.txt", "r", stdin);
   sc("%lld %lld", &n, &m);

   for(int i = 1; i <= n; i++){
      sc("%lld %lld", &red[i].l, &red[i].r);
      red[i].id = i;
      pom.val = red[i].l;
      pom.id = i;
      redovi.push(pom);
   }
   for(int i = 1; i <= m; i++){
      sc("%lld %lld", &kol[i].l, &kol[i].r);
      kol[i].id = i;

      pom.val = kol[i].l;
      pom.id = i;
      kolone.push(pom);
   }

   while(redovi.size() && kolone.size()){
      pom1 = redovi.top();
      pom2 = kolone.top();

      redovi.pop();
      kolone.pop();

      if(pom1.val != 0 && pom2.val != 0){
         v = min(pom1.val, pom2.val);
         pom1.val -= v;
         pom2.val -= v;

         br++;
         niz[br].c = pom2.id;
         niz[br].r = pom1.id;
         niz[br].n = v;

         suma+=v;


         red[niz[br].r].r -= v;
         kol[niz[br].c].r -= v;

         if(pom1.val == 0){
            kolone.push(pom2);
         }
         else{
            redovi.push(pom1);
         }
      }
      else if(pom1.val != 0){
         redovi.push(pom1);
      }
      else{
         kolone.push(pom2);
      }
   }


   while(redovi.size()) redovi.pop();
   while(kolone.size()) kolone.pop();


   for(int i = 1; i <= n; i++){
      pom.val = red[i].r;
      pom.id = i;
      redovi.push(pom);
   }
   for(int i = 1; i <= m; i++){
      pom.val = kol[i].r;
      pom.id = i;
      kolone.push(pom);
   }




   while(redovi.size() && kolone.size()){
      pom1 = redovi.top();
      pom2 = kolone.top();

      redovi.pop();
      kolone.pop();

      if(pom1.val != 0 && pom2.val != 0){
         v = min(pom1.val, pom2.val);
         pom1.val -= v;
         pom2.val -= v;

         br++;
         suma+=v;
         niz[br].c = pom2.id;
         niz[br].r = pom1.id;
         niz[br].n = v;

         if(pom1.val == 0){
            kolone.push(pom2);
         }
         else{
            redovi.push(pom1);
         }
      }
      else if(pom1.val != 0){
         redovi.push(pom1);
      }
      else{
         kolone.push(pom2);
      }
   }


   sort(niz+1, niz+1+br, cmp);


   int novibr = 0;
   for(int i = 1; i <= br; i++){
      if(niz[i].r==niz[i+1].r && niz[i].c==niz[i+1].c){
         niz[i+1].n += niz[i].n;
         niz[i].n = -1;
         continue;
      }
   }

   for(int i = 1; i <= br; i++){
      if(niz[i].n!=-1) novibr++;
   }
   pr("%lld\n", suma);
   pr("%lld\n", novibr);
   for(int i = 1; i <= br; i++){
      if(niz[i].n != -1) pr("%lld %lld %lld\n", niz[i].r, niz[i].c, niz[i].n);
   }

	return 0;
}