#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);
  vector<vector<int>> ans(n, vector<int>(m));
  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) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int x = min(a[i] - sx[i], c[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int x = min(a[i] - sx[i], d[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int x = min(b[i] - sx[i], d[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
  } else {
    for (int j = 0; j < m; j++) {
      for (int i = 0; i < n; i++) {
        int x = min(a[i] - sx[i], c[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
    for (int j = 0; j < m; j++) {
      for (int i = 0; i < n; i++) {
        int x = min(b[i] - sx[i], c[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
    for (int j = 0; j < m; j++) {
      for (int i = 0; i < n; i++) {
        int x = min(d[i] - sx[i], c[j] - sy[j]);
        ans[i][j] += x;
        sx[i] += x;
        sy[j] += x;
      }
    }
  }
  vector<array<int, 3>> ops;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      res += ans[i][j];
      if (ans[i][j] > 0) {
        ops.push_back({i + 1, j + 1, ans[i][j]});
      }
    }
  }
  cout << res << '\n';
  cout << ops.size() << '\n';
  for (auto& p : ops) {
    cout << p[0] << " " << p[1] << " " << p[2] << '\n';
  }
  return 0;
}