#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin >> n >> m;
    int No[n];
    int Mo[m];
    int field[n][m];

    int a,b[n],c,d[m];

    for(int i=0;i<n;i++){
        No[i]=0;
        cin >> a >> b[i];
    }

    for(int i=0;i<m;i++){
        Mo[i]=0;
        cin >> c >> d[i];
    }

    long long sum=0;
    int k=0;

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(b[i]-No[i]<d[j]-Mo[j]){
                field[i][j] = b[i]-No[i];
                Mo[j] += b[i]-No[i];
                No[i] = b[i];
            } else{
                field[i][j] = d[j]-Mo[j];
                No[i] += d[j]-Mo[j];
                Mo[i] = d[j];
            }
            if(field[i][j]>0) k++;
            sum+=field[i][j];
        }
    }

    cout << sum << endl;
    cout << k << endl;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(field[i][j]>0)
            cout << i+1 << " " << j+1 << " " << field[i][j] << endl;
        }
    }

    /*min strat*/

    //cout << "Hello world!" << endl;
    return 0;
}