#include <bits/stdc++.h>
#define ll int
ll polje[50000];
ll sumaniza(int niz[],int n){
    ll suma = 0;
    for(int i = 0;i<n;i++){
        suma+=niz[i];
    }
    return suma;
}
ll m(ll a,ll b){
    if(a<b)return a;
    else return b;
}
using namespace std;

int main()
{

    int N,M,MinN[50000],MaxN[50000],MinM[50000],MaxM[50000];
    scanf("%d%d",&N,&M);
    for(int i =0;i<N;i++){
        scanf("%d%d",&MinN[i],&MaxN[i]);
    }
    for(int i =0;i<M;i++){
        scanf("%d%d",&MinM[i],&MaxM[i]);
    }
    ll sumaN,sumaM;
    sumaN=sumaniza(MaxN,N);
    sumaM = sumaniza(MaxM,M);
    ll makskrompir=m(sumaN,sumaM);
    printf("%d\n",makskrompir);
    if(N==1){
        for(int i =0;i<M;i++){
            polje[i]=MinM[i];

        }
        int p = 0;
        while(sumaniza(polje,M)<makskrompir){
            if(polje[p]<MaxM[p]){
                polje[p]+=1;
            }
            else{
                p+=1;
            }
        }

    int r = 0;
    for(int i = 0;i<M;i++){
        if(polje[i]>0)r+=1;
    }
    printf("%d\n",r);
    for(int i = 0;i<M;i++){
        printf("1 %d %d\n",i+1,polje[i]);
    }
    }
}