#include <bits/stdc++.h>

#define ll long long
#define loop(i, s, e) for(ll i = s; i < e; i++)

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll n, m, a, b, c, d, res = 0;
    cin >> n >> m;

    unordered_map<ll, ll> polja;
    vector<ll> maxims(m);
    cin >> a >> b;
    loop(i, 0, m) {
        cin >> c >> d;
        a -= c;
        b -= c;
        d -= c;
        polja[i] += c;
        res += c;
        maxims[i] = d;
    }

    loop(i, 0, m) {
        if (b - maxims[i] >= 0) {
            b -= maxims[i];
            res += maxims[i];
            polja[i] += maxims[i];
        }    
    }

    cout << res << '\n';
    cout << polja.size() << '\n';
    for(const auto &x : polja) {
        cout << 1 << ' ' << x.first + 1 << ' ' << x.second << '\n';
    }

    return 0;
}

#include <bits/stdc++.h>

#define ll long long
#define loop(i, s, e) for(ll i = s; i < e; i++)

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll n, m, a, b, c, d, res = 0;
    cin >> n >> m;

    map<pair<ll, ll>, ll> polja;
    set<pair<ll, ll>, greater<pair<ll, ll>>> xmin;
    set<pair<ll, ll>, greater<pair<ll, ll>>> ymin;
    loop(i, 0, n) {
        cin >> a >> b;
        xmin.insert(make_pair(b, i));
    }
    loop(i, 0, m) {
        cin >> c >> d;
        ymin.insert(make_pair(d, i));
    }

    ll i = 0, j = 0;
    while(i < n && j < n) {
        auto xum = *xmin.begin();
        auto yum = *ymin.begin();

        ll mini = min(xum.first, yum.first);

        polja[make_pair(xum.second, yum.second)] += mini;

        res += mini;
        xmin.erase(xum);
        ymin.erase(yum);
        if (xum.first - mini > 0) {
            i++;
            xmin.insert(make_pair(xum.first - mini, xum.second));
        } else if (yum.first - mini > 0) {
            j++;
            ymin.insert(make_pair(yum.first - mini, yum.second));
        } else {
            i++;
            j++;
        }
    }

    cout << res << '\n';
    cout << polja.size() << '\n';
    for(const auto &x : polja) {
        cout << x.first.first + 1 << ' ' << x.first.second + 1 << ' ' << x.second << '\n';
    }

    return 0;
}