#include <iostream>
#include <bits/stdc++.h>
#define ll long long
#define F1 {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ld lond duble
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mp make_pair
using namespace std;
long long nzd (long long a, long long b)
{
    if(b==0)return a;
    a=a%b;
    return nzd(b,a);
}
bool prost (int n){

if(n==1)return false;
if(n==2)return true;
if(n%2==0)return false;
for(int i=3;i*i<=n;i+=2){
    if(n%i==0)return false;
}
return true;
}
void Eratosten (vector<bool>& prost, int n){
prost.resize(n+1,true);
prost[0]=prost[1]=false;
for(int i=2;i*i<=n;i++){
    if(prost[i]){
        for(int j=i*i;j<=n;j+=i)prost[j]=false;
    }
}}
void ProstiUIntervalu(int a, int b, int& broj, int& zbir ){
vector<bool> prost;
Eratosten(prost,b);
zbir=0;
broj=0;
for(int i=a;i<=b;i++){
    if(prost[i]){
        zbir=(zbir+i);
        broj++;
    }
}
}

int main()
{
    F1;
    int n,m;
    cin>>n>>m;
    vector<int>a(n+1);
    vector<int>b(n+1);
    vector<int>c(m+1);
    vector<int>d(m+1);
    for(int i=1;i<=n;i++)
        cin>>a[i]>>b[i];
     for(int j=1;j<=m;j++)
        cin>>c[j]>>d[j];
    int g[n+1][m+1];
    for(int q=1;q<=n;q++)
        for(int w=1;w<=m;w++)
        g[q][w]=0;
    int zbir=0;
    int br=0;
    if(n==1)
    { zbir=0;
    br=m;
       for(int y=1;y<=m;y++){
        g[n][y]=c[y];
        zbir+=g[n][y];
       }
       if(zbir==b[1]){
        cout<<zbir<<endl;
        for(int cc=1;cc<=m;cc++){
            if(g[n][cc]==0)br--;
        }
        cout<<br<<endl;
        for(int o=1;o<=m;o++){
           if(g[n][o]!=0) cout<<n<<" "<<o<<" "<<g[n][o]<<endl;}}
       else{
        for(int p=1;p<=m;p++){
        if(b[1]>zbir+(d[p]-c[p])){
           zbir+=(d[p]-c[p]);
            g[n][p]=d[p];
            }
        else{g[n][p]=(b[1]-zbir);
        zbir=b[1];}
        }
        if(zbir==b[1]){
        cout<<zbir<<endl;
        for(int vc=1;vc<=m;vc++){
            if(g[n][vc]==0)br--;
        }
        cout<<br;
        for(int u=1;u<=m;u++){
           if(g[n][u]!=0) cout<<n<<" "<<u<<" "<<g[n][u]<<endl;}
       }}
    }
    return 0;
}