#include <bits/stdc++.h>
using namespace std;

int n, m;
int ogrRed[50005][2]; //u zadatku, a i b
int ogrKol[50005][2]; //u zadatku, c i d

//resenja var
int brKrompira;
int brPolja;

//sdadjidgfsojigfdsokji
int kolSum;
int kolMax;
int nuleRed, nuleKol;

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d %d", &ogrRed[i][0], &ogrRed[i][1]);
	}
	for (int i = 1; i <= m; ++i)
	{
		scanf("%d %d", &ogrKol[i][0], &ogrKol[i][1]);
	}

	for (int i = 1; i <= n; ++i)
	{
		if (ogrRed[i][1] == 0)
		{
			continue;
		}
		for (int j = 1; j <= m; ++j)
		{
            if (ogrKol[j][1] == 0)
			{
				continue;
			}
			brPolja++;
			brKrompira+=min(ogrRed[i][1], ogrKol[j][1]);
		}
	}
	printf("%d\n", brKrompira);
	printf("%d\n", brPolja);
	for (int i = 1; i <= n; ++i)
	{
		if (ogrRed[i][1] == 0)
		{
			continue;
		}
		for (int j = 1; j <= m; ++j)
		{
            if (ogrKol[j][1] == 0)
			{
				continue;
			}
			printf("%d %d %d\n", i, j, min(ogrRed[i][1], ogrKol[j][1]));
		}
	}



	return 0;
}