#include <iostream>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::min;

inline bool in_range(int interv1, int interv2, int trazeno) {
    return (trazeno > interv1 && trazeno < interv2);
}

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

    int N, M, sumared = 0;
    cin >> N >> M;

    std::vector<std::pair<int, int>> poljared(N),poljakol(M);
    std::vector<int> stanjered(N), stanjekol(M);

    //ubacivanje podataka,racunanje ukupne sume redova i kolona,popunjavanje polja i kolona najvisim vrednostima
    for (auto i = 0; i < N; i++) {
        cin >> poljared[i].first >> poljared[i].second;
        stanjered[i] = poljared[i].second;
        sumared += stanjered[i];
    }
    for (auto i = 0; i < M; i++) {
        cin >> poljakol[i].first >> poljakol[i].second;
    }

    for (auto i = 0; i < M; i++) {
        stanjekol[i] = sumared;
        if (!in_range(poljakol[i].first,poljakol[i].second,stanjekol[i])) {
            stanjekol[i] = min(poljakol[i].second, sumared - poljakol[i].second);
            sumared -= stanjekol[i];
        }
    }
    int res = 0;
    for (auto i = 0; i < N; i++) {
        res += stanjered[i];
    }
    cout << res << '\n';
    for (auto i = 0; i < N; i++) {
        for (auto k = 0; k < M; k++) {
            if (stanjered[i] + stanjekol[k]) {
                cout << i << ' ' << k << ' ' << stanjered[i] + stanjekol[k] << '\n';
            }
        }
    }
}