#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define fr front()

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n, m; cin >> n >> m;
	vector<ll> min_rows(n), max_rows(n);
	vector<ll> min_cols(m), max_cols(m);
	for (int i = 0; i < n; i++)
		cin >> min_rows[i] >> max_rows[i];
	for (int i = 0; i < m; i++)
		cin >> min_cols[i] >> max_cols[i];

	queue<pair<int, ll>> remaining_cols, remaining_rows;
	for (int i = 0; i < n; i++)
		remaining_rows.push({ i+1, min_rows[i] });
	for (int i = 0; i < n; i++)
		remaining_rows.push({ i+1, max_rows[i] - min_rows[i] });

	for (int i = 0; i < m; i++)
		remaining_cols.push({ i+1, min_cols[i] });
	for (int i = 0; i < m; i++)
		remaining_cols.push({ i+1, max_cols[i] - min_cols[i] });

	map<pair<int, int>, ll> sol;
	while (!remaining_cols.empty() && !remaining_rows.empty())
	{
		int row = remaining_rows.fr.fi;
		while (remaining_rows.fr.se > 0 && !remaining_cols.empty())
		{
			int col = remaining_cols.fr.fi;
			ll to_add = min(remaining_rows.fr.se, remaining_cols.fr.se);
			sol[{row, col}] += to_add;
			remaining_rows.fr.se -= to_add;
			remaining_cols.fr.se -= to_add;
			if (remaining_cols.fr.se  == 0)
				remaining_cols.pop();
		}
		remaining_rows.pop();
	}
	ll sum_added = 0;
	int cnt_nonzero = 0;
	for (auto x : sol)
	{
		if (x.se)
			cnt_nonzero++;
		sum_added += x.se;
	}
	cout << sum_added << endl;
	cout << cnt_nonzero << endl;
	for (auto x : sol)
		if (x.se)
			cout << x.fi.fi << " " << x.fi.se << " " << x.se << endl;

	return 0;
}