#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;}
int n, m, a[mxN], b[mxN], c[mxN], d[mxN], br, v;
ll suma;
struct polje{
   int r, c, n;
}niz[1000006];

int main(){
//   freopen("in.txt", "r", stdin);
   bool test1=0;
   sc("%d %d", &n, &m);

   for(int i = 1; i <= n; i++){
      sc("%d %d", &a[i], &b[i]);
   }
   for(int i = 1; i <= m; i++){
      sc("%d %d", &c[i], &d[i]);
   }
   if(n==m){
      test1 = 1;
      for(int i=1; i<=n; i++){
         if( !(a[i] <= c[i] && c[i] <= b[i] && b[i] <= d[i]) ) test1 = 0;
      }
      if(test1){
//         cout<<"ovde sam\n";
         for(int i = 1; i <= n; i++){
            v = min(c[i], b[1]);
            if(v==0) break;
            b[1] -= v;
            br++;
            niz[br].c = i;
            niz[br].r = 1;
            niz[br].n = v;
            suma+=v;
         }
      }
   }
   else{
      for(int i = 1; i <= n; i++){
         for(int j = 1; j <= m; j++){
            int g1 = min(b[i], d[j]);
            int g2 = max(a[i], c[j]);
   //         cout << i <<", "<<j<<" : "<< g2 <<" do "<<g1<<endl;
            if(g2 > g1) continue;

            v = g1;

            if(v <= 0) continue;
            a[i] -= v;
            b[i] -= v;
            c[j] -= v;
            d[j] -= v;
            br++;
            niz[br].r = i;
            niz[br].c = j;
            niz[br].n = v;
            suma += v;
         }
      }
   }

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


	return 0;
}