#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  vector<int> c(m), d(m);
  for (int i = 0; i < m; i++) {
    cin >> c[i] >> d[i];
  }
  vector<int> sx(n);
  vector<int> sy(m);
  map<pair<int, int>, int> ans;
  long long sa = 0;
  long long sc = 0;
  for (int i = 0; i < n; i++) {
    sa += a[i];
  }
  for (int i = 0; i < m; i++) {
    sc += c[i];
  }
  if (sa >= sc) {
    int ptr = 0;
    for (int i = 0; i < n; i++) {
      while (ptr < m) {
        int x = min(a[i] - sx[i], c[ptr] - sy[ptr]);
        ans[{i, ptr}] += x;
        sx[i] += x;
        sy[ptr] += x;
        if (a[i] != sx[i]) {
          ptr++;
        } else {
          break;
        }
      }
    }
    ptr = 0;
    for (int i = 0; i < n; i++) {
      while (ptr < m) {
        int x = min(a[i] - sx[i], d[ptr] - sy[ptr]);
        ans[{i, ptr}] += x;
        sx[i] += x;
        sy[ptr] += x;
        if (a[i] != sx[i]) {
          ptr++;
        } else {
          break;
        }
      }
    }
    ptr = 0;
    for (int i = 0; i < n; i++) {
      while (ptr < m) {
        int x = min(b[i] - sx[i], d[ptr] - sy[ptr]);
        ans[{i, ptr}] += x;
        sx[i] += x;
        sy[ptr] += x;
        if (b[i] != sx[i]) {
          ptr++;
        } else {
          break;
        }
      }
    }
  } else {
    int ptr = 0;
    for (int j = 0; j < m; j++) {
      while (ptr < n) {
        int x = min(a[ptr] - sx[ptr], c[j] - sy[j]);
        ans[{ptr, j}] += x;
        sx[ptr] += x;
        sy[j] += x;
        if (c[j] != sy[j]) {
          ptr++;
        } else {
          break;
        }
      }
    }
    ptr = 0;
    for (int j = 0; j < m; j++) {
      while (ptr < n) {
        int x = min(b[ptr] - sx[ptr], c[j] - sy[j]);
        ans[{ptr, j}] += x;
        sx[ptr] += x;
        sy[j] += x;
        if (c[j] != sy[j]) {
          ptr++;
        } else {
          break;
        }
      }
    }
    ptr = 0;
    for (int j = 0; j < m; j++) {
      while (ptr < n) {
        int x = min(b[ptr] - sx[ptr], d[j] - sy[j]);
        ans[{ptr, j}] += x;
        sx[ptr] += x;
        sy[j] += x;
        if (d[j] != sy[j]) {
          ptr++;
        } else {
          break;
        }
      }
    }
  }
  vector<array<int, 3>> ops;
  long long res = 0;
  for (auto& p : ans) {
    int i = p.first.first;
    int j = p.first.second;
    int v = p.second;
    res += v;
    if (v > 0) {
      ops.push_back({i + 1, j + 1, v});
    }
  }
  cout << res << '\n';
  cout << ops.size() << '\n';
  for (auto& p : ops) {
    cout << p[0] << " " << p[1] << " " << p[2] << '\n';
  }
  return 0;
}