#include <bits/stdc++.h>
#define ll long long
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;
}
ll zbirm(int m[][50000],int n,int x){
    ll suma = 0;
    for(int i=0;i<n;i++){
        for(int j = 0;j<x;j++){
            suma+=m[i][j];
        }
    }
    return suma;
}
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);
    int polje[50000][50000];
    printf("%ll",makskrompir);
    if(N=1){
        for(int i =0;i<M;i++){
            polje[0][i]=MinM[i];

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

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