#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	cin.tie(NULL);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	pair<int, int> a[n], b[m];
	bool c = true;
	for (int i = 0; i < n; i++) {
		cin >> a[i].first >> a[i].second;
		if(a[i].first != a[i].second) c = false;
	}
	for (int i = 0; i < m; i++){
		cin >> b[i].first >> b[i].second;
	} 
	if (n == 1){
		int x = 0, k = 0;
		for (int i = 0; i < m; i++) {
			x += b[i].second;
			if(b[i].second == 0) k++;
		}
		if(x < a[0].second){
			cout << x << '\n';
			cout << m - k << '\n';
			for (int i = 0; i < m; i++) {
				if(b[i].second != 0) cout << "1 " << i << ' ' << b[i].second << '\n';
			}
		} 
		else {
			cout << a[0].second << '\n';
			a[0].second -= x;
			cout << m - k << '\n';
			for (int i = 0; i < m; i++) {
				int y = min(b[i].second - b[i].first, a[0].second);
				if(b[i].second != 0) cout << "1 " << i << ' ' << b[i].first + y << '\n';
				a[0].second -= y;
			}
		}
	}
	else if (n == m){
		int c[n][m] = {0}, d = 0, e = 0;
		for(int i = 0; i < n; i++) {
			c[i][i] = min(a[i].second, b[i].second);
			if (c[i][i]) {
				d++;
				e += c[i][i];
			}
		}
		cout << e << '\n';
		cout << d << '\n';
		for (int i = 0; i < n; i++) {
			cout << i << ' ' << i << ' ' << c[i][i] << '\n';
		}
	}
	else{
		int c[n][m] = {0}, d = 0, e = 0;
		for(int i = 0; i < n; i++) {
			c[i][i] = min(a[i].second, b[i].second);
			if (c[i][i]) {
				d++;
				e += c[i][i];
			}
		}
		cout << e << '\n';
		cout << d << '\n';
		for (int i = 0; i < n; i++) {
			cout << i << ' ' << i << ' ' << c[i][i] << '\n';
		}
	}
	return 0;
}