#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <cmath>
#include <limits>

struct interval {
    short start;
    short end;
    interval(short _s = 0, short _e = 0) {
        start = _s;
        end = _e;
    }
};

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    short n, m;
    std::cin >> n >> m;
    std::vector<interval> red(n);
    std::vector<interval> kolona(m);

    for (int i = 0; i < n; i++) {
        short a, b;
        std::cin >> a >> b;
        red[i] = interval(a, b);
    }
    for (int i = 0; i < m; i++) {
        short c, d;
        std::cin >> c >> d;
        kolona[i] = interval(c, d);
    }
    std::vector<std::vector<short>> k(n, std::vector<short>(m, 0));
    std::vector<short> sumi(n, 0); //suma reda j
    std::vector<short> sumj(m, 0); // suma kolone i

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            k[i][j] = std::min(red[i].start, kolona[j].start);
            sumi[i] += k[i][j];
            sumj[j] += k[i][j];
        }
    }

    short res = 0;
    short res2 = 0;
    //std::vector<std::pair<interval, int>> res2;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            //std::cout << "[" << red[i].start << " " << red[i].end << "] [" << kolona[i].start << " " << kolona[i].end << "] [" << sumi[i] << " " << sumj[j] << "] " << k[i][j] << " ";

            if (!(red[i].start <= sumi[i] && red[i].end >= sumi[i]) || !(kolona[j].start <= sumj[j] && kolona[j].end >= sumj[j])) {
                if (red[i].end < sumi[i] || kolona[j].end < sumj[j]) {
                    while ((red[i].end < sumi[i] || kolona[j].end < sumj[j]) && k[i][j] > 0 && sumi[i] > red[i].start && sumj[j] > kolona[j].start) {
                        k[i][j]--;
                        sumi[i]--;
                        sumj[i]--;
                    }
                }
                /*while ((!(red[i].start <= sumi[i] && red[i].end >= sumi[i]) || !(kolona[j].start <= sumj[j] && kolona[j].end >= sumj[j])) && k[i][j] > 0) {
                }*/
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            while (red[i].end > sumi[i] && kolona[j].end > sumj[j]) {
                k[i][j]++;
                sumi[i]++;
                sumj[i]++;
            }
            res += k[i][j];
            res2 += k[i][j] >= 1;
        }
    }

    std::cout << res << std::endl;
    std::cout << res2 << std::endl;

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