#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
	int n, m;
	cin >> n >> m;
	vector<pair<int, int>>a(n), b(m);
	vector<vector<int>>p(n, vector<int>(m));
	for (int i = 0; i < n; i++)
	{
		int x, y; cin >> x >> y;
		a[i] = { x, y };
	}
	for (int i = 0; i < m; i++)
	{
		int x, y; cin >> x >> y;
		b[i] = { x,y };
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (min(a[i].second, b[j].second) >= max(a[i].first, b[j].first)) p[i][j] = max(a[i].first, b[j].first);
			else p[i][j] = min(a[i].second, b[j].second);
			a[i] = {max(0, a[i].first - p[i][j]), max(0,a[i].second - p[i][j] )};
			b[j] = {max(0, b[j].first - p[i][j]),max(0, b[j].second - p[i][j] )};
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			p[i][j] += min(a[i].second, b[j].second);
			a[i] = { max(0, a[i].first - p[i][j]), max(0,a[i].second - p[i][j]) };
			b[j] = { max(0, b[j].first - p[i][j]),max(0, b[j].second - p[i][j]) };
		}
	}
	long long z = 0;
	vector<pair<int, int>>tamogdeima;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			z += p[i][j];
			if (p[i][j] != 0) tamogdeima.push_back({ i, j });
		}
	}
	cout << z << endl << tamogdeima.size() << endl;
	for (int i = 0; i < tamogdeima.size(); i++)
	{
		cout << tamogdeima[i].first + 1 << " " << tamogdeima[i].second + 1 << " " << p[tamogdeima[i].first][tamogdeima[i].second] << endl;
	}
}