#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && c < 48) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
  return f * x;
}
const int maxn = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, m;
struct Line {
  int x, y;
};
vector<Line> h0, h1, v0, v1;
long long ans;
int b[maxn];
inline void add(int x, int d) {
  x++;
  while (x < maxn) b[x] += d, x += x & -x;
}
inline int get(int x) {
  x++;
  int s = 0;
  while (x) s += b[x], x -= x & -x;
  return s;
}
void solve(vector<Line> h, vector<Line> v) {
  sort(h.begin(), h.end(),
       [&](const Line &a, const Line &b) { return a.y < b.y; });
  sort(v.begin(), v.end(),
       [&](const Line &a, const Line &b) { return a.y < b.y; });
  for (Line t : v) add(t.x, 1);
  int i = 0;
  for (Line t : h) {
    while (i < v.size() && v[i].y < t.y) add(v[i++].x, -1);
    if (i == v.size()) break;
    ans += get(t.x);
  }
  memset(b, 0, sizeof(b));
}
int main() {
  n = read(), m = read();
  v0.push_back((Line){0, 1000000});
  v0.push_back((Line){1000000, 1000000});
  h0.push_back((Line){1000000, 0});
  h0.push_back((Line){1000000, 1000000});
  for (int i = 1; i <= n; ++i) {
    int y = read(), l = read(), r = read();
    if (l == 0)
      h0.push_back((Line){r, y});
    else
      h1.push_back((Line){l, y});
  }
  for (int i = 1; i <= m; ++i) {
    int x = read(), l = read(), r = read();
    if (l == 0)
      v0.push_back((Line){x, r});
    else
      v1.push_back((Line){x, l});
  }
  ans = 0;
  solve(h0, v0);
  for (Line &t : h0) t.y = 1000000 - t.y;
  for (Line &t : v1) t.y = 1000000 - t.y;
  solve(h0, v1);
  for (Line &t : h0) t.y = 1000000 - t.y;
  for (Line &t : v1) t.y = 1000000 - t.y;
  for (Line &t : v0) t.x = 1000000 - t.x;
  for (Line &t : v1) t.x = 1000000 - t.x;
  for (Line &t : h1) t.x = 1000000 - t.x;
  solve(h1, v0);
  for (Line &t : h1) t.y = 1000000 - t.y;
  for (Line &t : v1) t.y = 1000000 - t.y;
  solve(h1, v1);
  cout << ans - 4 - n - m + 1 << endl;
}
