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

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

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

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

    for (int i = 0; i < n; i++) {
        int a, b;
        std::cin >> a >> b;
        red[i] = interval(a, b);
    }
    for (int i = 0; i < m; i++) {
        int c, d;
        std::cin >> c >> d;
        kolona[i] = interval(c, d);
    }
    std::vector<std::vector<int>> k(n, std::vector<int>(m, 0));
    std::vector<int> sumi(n, 0); //suma reda j
    std::vector<int> 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];
        }
    }

    int res = 0;
    int 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;
            }
        }
    }
}