#include <bits/stdc++.h>

using namespace std;
#define f first
#define s second
int main() {
	int n, m;
	cin >> n >> m;
	vector<pair<long long, long long>> v(n), k;
	for(int i = 0;i < n;i++)
		cin >> v[i].f >> v[i].s;
	for(int i = 0;i < m;i++) {
		int x, y;
		cin >> x >> y;
		if(x == 0 && y == 0)
			continue;
		k.push_back({x, y});
	}
	m = k.size();
	vector<long long> p(m);
	if(!k.empty())
		p[0] = k[0].s;
	for(int i = 1;i < m;i++)
		p[i] = p[i - 1] + k[i].s;
	int id =  -1;
	long long od = 0;
	vector<long long> vrsta, kolona, zbir;
	long long ans = 0;
	for(int i = 0;i < n;i++) {
		int l = id + 1, r = m - 1, res = 0;
		while(l <= r) {
			int mid = (l + r) / 2;
			if(p[mid] - od <= v[i].s) {
				res = mid;
				l = mid + 1;
			}
			else
				r = mid - 1;
		}
		long long temp = od, id1 = id;
		if(p[res] - od > v[i].s) 
			id1 = res - 1;
		else
			id1 = res;
		for(int j = id + 1;j <= res;j++) {
			long long x = min(v[i].s, k[j].s); 
			vrsta.push_back(i + 1);
			kolona.push_back(j + 1);
			zbir.push_back(x);
			ans += x;
			k[j].s -= x;
			v[i].s -= x;
			od += x;
		}
		id = id1;
	}
	cout << ans << endl << kolona.size() << endl;
	for(int i = 0;i < kolona.size();i++)
		cout << vrsta[i] << " " << kolona[i] << " " << zbir[i] << endl;
	return 0;
}