#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
struct po{
	int x;
	int y;
	ull n;
};
int main()
{
	int n, m;
	vector<pair<ull, ull>>a, b;
	ull o, p;
	pair<ull, ull>u;
	int i, j;
	cin >> n >> m;
	vector<vector<ull>>cc(n);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			cc[i].push_back(0);
	ull br1=0, br2=0;
	for (i = 0; i < n; i++)
	{
		cin >> o >> p;
		u.first = o;
		u.second = p-o;
		a.push_back(u);
		br1 += p;
	}
	for (i = 0; i < m; i++)
	{
		cin >> o >> p;
		u.first = o;
		u.second = p-o;
		b.push_back(u);
		br2 += p;
	}
	int brp = 0;
	for (i = 0; i < n; i++)
	{
		j = 0;
		while (j < m || a[i].first != 0)
		{
			if (b[j].first != 0) {
				brp++;
				if (a[i].first > b[j].first) {
					a[i].first -= b[j].first;
					cc[i][j] = b[j].first;
					b[j].first = 0;
				}
				else {
					b[j].first -= a[i].first;
					cc[i][j] = a[i].first;
					a[i].first = 0;
				}
			}
			j++;
		}
		if (a[i].first != 0)
        {
            j=0;
		while (a[i].first != 0)
		{
			if (b[j].second != 0) {
				if (a[i].first > b[j].second) {
					a[i].first -= b[j].second;
					cc[i][j] += b[j].second;
					b[j].second = 0;
				}
				else {
					b[j].second -= a[i].first;
					cc[i][j] += a[i].first;
					a[i].first = 0;
				}
			}
			j++;
		}

        }
	}for (i = 0; i < n; i++)
	{
		j = 0;
		while (j < m || a[i].second != 0)
		{
			if (b[j].second != 0) {
				if (a[i].second > b[j].second) {
					a[i].second -= b[j].second;
					cc[i][j] += b[j].second;
					b[j].second = 0;
				}
				else {
					b[j].second -= a[i].second;
					cc[i][j] += a[i].second;
					a[i].second = 0;
				}
			}
			j++;
		}
	}
	if (br1 > br2) // vise u n
	{
		cout << br2 << endl;
	}
	else
	{
		cout << br1 << endl;
	}

	cout << brp << endl;

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