#include <bits\stdc++.h>

#define FAST {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
#define in insert
#define er erase
#define pb push_back
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define maxn 50005

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/

using namespace std;

ll a[maxn], b[maxn], c[maxn], d[maxn];

struct tri{
    ll x, y, koliko;
};

int main()
{
    vector<tri> resenje;
    FAST
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> c[i] >> d[i];
    }
    if(n == 1){
        ll maxi = 0;
        ll mini = 0;
        for(int i = 1; i <= m; i++){
            maxi += d[i];
            mini += c[i];
        }
        ll rez =  min(b[1], maxi);
        cout << rez << endl;
        ll razlika = rez - mini;
        for(int i = 1; i <= m; i++){
            tri k;
            k.x = 1;
            k.y = i;
            k.koliko = c[i] + min(d[i] - c[i], razlika);
            razlika -= (k.koliko - c[i]);
            if(k.koliko > 0){
                resenje.pb(k);
            }
        }
        cout << resenje.size() << endl;
        for(int i = 0; i < resenje.size(); i++){
            cout << resenje[i].x << " " << resenje[i].y << " " << resenje[i].koliko << endl;
        }
    }

    return 0;
}