#include <bits/stdc++.h>

using namespace std;

struct granica{
    int granica1;
    int granica2;
};

struct indeksi{
    int indeks1;
    int indeks2;
    int krompir;
};

int main()
{
    int n,m;
    cin >> n >> m;
    vector<granica> redovi;
    vector<granica> kolone;
    for(int i = 0;i<n;i++){
        int a,b;
        cin >> a >> b;
        redovi.push_back({a,b});
    }
    int maks=0;
    int current_max=0;
    for(int i = 0;i<m;i++){
        int a,b;
        cin >> a >> b;
        kolone.push_back({a,b});
    }
    int i = 0;
    int s = 0;
    int SUMA=0;
    int br = 0;
    vector<indeksi> index;
    int preostalo = 0;
    preostalo = redovi[s].granica2-maks;
    while(true){
        int k = max(kolone[i].granica2,redovi[s].granica2);
        maks = min(k,preostalo);
        preostalo = preostalo-maks;
        if(maks<0){
            maks = 0;
        }else{
            if(maks!=0){
                index.push_back({s+1,i+1,maks});
                br++;
            }
        }
        if(maks>current_max){
            current_max=maks;
        }
        if(i!=kolone.size()-1){
            i++;
        }else if(s!=redovi.size()-1){
            s++;
            i=0;
            preostalo = redovi[s].granica2-maks;
        }else{
            break;
        }
        SUMA+=maks;
    }
    cout << SUMA << endl;
    cout << br << endl;
    for(int i = 0;i<index.size();i++){
        cout << index[i].indeks1 << " " << index[i].indeks2 << " "<< index[i].krompir << endl;
    }
    return 0;
}