#include <iostream>
#include <vector>
using namespace std;
struct pamti
{
    int x,y,n;
};
int main()
{
    int m,n,s=0,br=0;
    cin>>n>>m;
    int a[n],b[n],c[m],d[m];
    vector<pamti>v;
    for(int i=0;i<n;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<m;i++)
        cin>>c[i]>>d[i];

    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(b[i]>=d[j])
            {
            s+=d[j];
            b[i]-=d[j];
            a[i]-=d[j];
            if(d[j]>0)
            {
            br++;
            pamti r;
            r.x=i;
            r.y=j;
            r.n=d[j];
            v.push_back(r);
            }
            }
            else
            {
            s+=b[i];
            d[j]-=b[i];
            c[j]-=b[i];
            if(b[i]>0)
            {
            br++;
            pamti r;
            r.x=i;
            r.y=j;
            r.n=b[i];
            v.push_back(r);
            }
            }
        }
        cout<<s<<endl;
        cout<<br<<endl;
        for(int i=0;i<v.size();i++)
        {
            cout<<v[i].x+1<<" "<<v[i].y+1<<" "<<v[i].n<<endl;
        }
}