#include <bits/stdc++.h>

using namespace std;

int n,m,c=1;
long long A[50010],B[50010],C[50010],D[50010],u1,u2,g,v;
vector<pair<pair<int,int>,long long>> V;

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld %lld",&A[i],&B[i]);
        u1 += B[i];
    }
    for(int i=1; i<=m; i++)
    {
        scanf("%lld %lld",&C[i],&D[i]);
        u2 += D[i];
    }
    if(u1<=u2)
    {
        printf("%lld\n",u1);
        for(int i=1; i<=n; i++)
        {
            while(B[i])
            {
                v = max(0ll,min(u2-u1,D[c]-C[c]));
                u2 -= v;
                D[c] -= v;
                g = min(B[i],D[c]);
                V.push_back({{i,c},g});
                u1 -= g;
                B[i] -= g;
                u2 -= g;
                D[c] -= g;
                if(!D[c]) c++;
            }
        }
    }
    else
    {
        printf("%lld\n",u2);
        for(int i=1; i<=m; i++)
        {
            while(D[i])
            {
                v = max(0ll,min(u1-u2,B[c]-A[c]));
                u1 -= v;
                B[c] -= v;
                g = min(D[i],B[c]);
                V.push_back({{c,i},g});
                u2 -= g;
                D[i] -= g;
                u1 -= g;
                B[c] -= g;
                if(!B[c]) c++;
            }
        }
    }
    printf("%d\n",V.size());
    for(int i=0; i<V.size(); i++)
    {
        printf("%d %d %lld\n",V[i].first.first,V[i].first.second,V[i].second);
    }
    return 0;
}