#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;

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;br=m;
        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;break;}

}

       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 if(b[1]==zbir+(d[p]-c[p])){
                zbir=b[1];
                g[n][p]=d[p];
                cout<<zbir<<endl;
        br=m;
        for(int cg=1;cg<=m;cg++){
            if(g[n][cg]==0)br--;
        }
        cout<<br<<endl;
        for(int r=1;r<=m;r++){
           if(g[n][r]!=0) cout<<n<<" "<<r<<" "<<g[n][r]<<endl;}
            break;

                }
               else{g[n][p]+=(b[1]-zbir);
        zbir=b[1];
        cout<<zbir<<endl;br=m;
        for(int vc=1;vc<=m;vc++){
            if(g[n][vc]==0)br--;
        }
        cout<<br<<endl;
        for(int u=1;u<=m;u++){
           if(g[n][u]!=0) cout<<n<<" "<<u<<" "<<g[n][u]<<endl;}break;
       }}
       if(zbir!=b[1]){cout<<zbir<<endl;
        for(int tc=1;tc<=m;tc++){
            if(g[n][tc]==0)br--;
        }
        cout<<br<<endl;
        for(int t=1;t<=m;t++){
           if(g[n][t]!=0) cout<<n<<" "<<t<<" "<<g[n][t]<<endl;}}
           }
    }
    return 0;
}