#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int m;
    long long sumn=0;
    long long summ=0;
    scanf("%d",&n);
    scanf("%d",&m);
    pair<int,int>redovi[n+1];
    pair<int,int>kolone[m+1];
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&redovi[i].first,&redovi[i].second);
        sumn+=redovi[i].second;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&kolone[i].first,&kolone[i].second);
        summ+=kolone[i].second;
    }
    int mn=min(m,n);
   /* int x=0;
    for(int i=1;i<=nm;i++)
    {
        if(redovi[i].second>kolone[i].second)x++;
    }
    int km;
    int rm;
    int brpolj;
    if(m>n)
    {
        km=n-x;
        rm=x;
        brpolj=n+rm;
    }
    else
    {
        km=m-x;
        rm=x;
        brpolj=m+km;
    }*/
    long long mxkr=min(sumn,summ);
    if(n==1)
    {
        if(sumn>=summ)
        {
            printf("%lld\n",summ);
            printf("%d\n",m);
            for(int i=1;i<=m;i++)
            {
                printf("%d %d %d\n",1,i,kolone[i].second);
            }
        }
        else
        {
            printf("%lld\n",sumn);
            int niz[m+1]={0};
            long long suma=0;
            int i=1;
            int brp=0;
            while(suma<sumn)
            {
                if(suma+kolone[i].second<=sumn)
                {
                    niz[i]=kolone[i].second;
                    suma+=kolone[i].second;
                    brp++;
                    i++;
                }
                else
                {
                    niz[i]=sumn-suma;
                    suma=sumn;
                    brp++;
                }
            }
            printf("%d\n",brp);
            for(int j=1;j<=brp;j++)
            {
                printf("%d %d %d\n",1,j,niz[j]);
            }
        }
    }
    else
    {
    printf("%lld\n",mxkr);
    printf("%d\n",n);
    int x[n+1]={0};
    int y[n+1]={0};
    int kl[n+1]={0};
    for(int i=1;i<=mn;i++)
    {
        x[i]=i;
        y[i]=i;
        kl[i]=min(kolone[i].second,redovi[i].second);
        printf("%d %d %d\n",x[i],y[i],kl[i]);
    }
    }
    return 0;
}