#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m;
	cin >> n >> m;
	int a[n];
	int c[m];
	for(int i = 0; i < n; i++)
	{
		int y;
		cin >> a[i] >> y;
		a[i] += y-a[i];
	}
	for(int i = 0; i < m; i++)
	{
		int y;
		cin >> c[i] >>y;
		c[i]+=y-c[i];
	}
	int max = 0;
	int k = 0;
	int novi_niz[n][m] = {0,0};
	memset(novi_niz, 0, sizeof(novi_niz));
	for(int i = 0; i < n; i++)
	{
		if(!a[i]) continue;

		for(int j = 0; j < m; j++)
		{
			if(a[i] < 1)
				break;
			if(c[j] < 1)
				continue;
			int old_max = max;
			if(a[i] > c[j])
			{
				max+=c[j];
				a[i]-=c[j];
				c[j] = 0;
			}
			else if(a[i] == c[j])
			{
				max+=c[j];
				a[i] = c[j] = 0;
			}
			else
			{
				max+=a[i];
				c[j]-=a[i];
				a[i] =0;	
			}
			k++;
			novi_niz[i][j] += max-old_max;
		}

	}

	cout << max << endl << k << endl;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(novi_niz[i][j])
				cout << i+1 << " " << j+1 << " " << novi_niz[i][j] << "\n";
	return 0;
}