#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1e9;
long long N, M;
long long A[1005], B[1005], C[1005];
long long D[1005], E[1005], F[1005];
vector<long long> Hcmp, Wcmp;
vector<vector<bool>> Right, Up;
struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<long long> Size;
  UnionFind(int n = 1) { init(n); }
  void init(int n = 1) {
    par.resize(n + 1);
    rank.resize(n + 1);
    Size.resize(n + 1);
    for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      return par[x] = r;
    }
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y);
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    Size[x] += Size[y];
    return true;
  }
  long long size(int x) { return Size[root(x)]; }
};
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
    Hcmp.push_back(A[i]);
    Hcmp.push_back(B[i]);
    Wcmp.push_back(C[i] - 1);
    Wcmp.push_back(C[i]);
    Wcmp.push_back(C[i] + 1);
  }
  for (int i = 0; i < M; i++) {
    cin >> D[i] >> E[i] >> F[i];
    Hcmp.push_back(D[i] - 1);
    Hcmp.push_back(D[i]);
    Hcmp.push_back(D[i] + 1);
    Wcmp.push_back(E[i]);
    Wcmp.push_back(F[i]);
  }
  Hcmp.push_back(-INF - 1);
  Wcmp.push_back(-INF - 1);
  sort(Hcmp.begin(), Hcmp.end());
  sort(Wcmp.begin(), Wcmp.end());
  Hcmp.erase(unique(Hcmp.begin(), Hcmp.end()), Hcmp.end());
  Wcmp.erase(unique(Wcmp.begin(), Wcmp.end()), Wcmp.end());
  for (auto h : Hcmp) cerr << h << " ";
  cerr << "\n";
  for (auto w : Wcmp) cerr << w << " ";
  cerr << "\n";
  long long H = Hcmp.size();
  long long W = Wcmp.size();
  cerr << "H: " << H << "\n";
  cerr << "W: " << W << "\n";
  Right.resize(H, vector<bool>(W, false));
  Up.resize(H, vector<bool>(W, false));
  for (int i = 0; i < N; i++) {
    auto itr = lower_bound(Hcmp.begin(), Hcmp.end(), A[i]);
    A[i] = itr - Hcmp.begin();
    itr = lower_bound(Hcmp.begin(), Hcmp.end(), B[i]);
    B[i] = itr - Hcmp.begin();
    itr = lower_bound(Wcmp.begin(), Wcmp.end(), C[i]);
    C[i] = itr - Wcmp.begin();
  }
  for (int i = 0; i < M; i++) {
    auto itr = lower_bound(Hcmp.begin(), Hcmp.end(), D[i]);
    D[i] = itr - Hcmp.begin();
    itr = lower_bound(Wcmp.begin(), Wcmp.end(), E[i]);
    E[i] = itr - Wcmp.begin();
    itr = lower_bound(Wcmp.begin(), Wcmp.end(), F[i]);
    F[i] = itr - Wcmp.begin();
  }
  UnionFind uni(H * W);
  long long sumsize = 0;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      long long val = 0;
      if (h != H - 1 and w != W - 1) {
        val = (Hcmp[h + 1] - Hcmp[h]) * (Wcmp[w + 1] - Wcmp[w]);
      }
      uni.Size[h * W + w] = val;
      sumsize += val;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = A[i]; j < B[i]; j++) {
      Right[j][C[i]] = true;
      cerr << "right: " << j << " " << C[i] << "\n";
    }
  }
  for (int i = 0; i < M; i++) {
    for (int j = E[i]; j < F[i]; j++) {
      Up[D[i]][j] = true;
      cerr << "up: " << D[i] << " " << j << "\n";
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i != H - 1 and !Up[i][j]) {
        uni.merge((i + 1) * W + j, i * W + j);
        cerr << "upmerge: " << i << " " << j << "\n";
      }
      if (j != W - 1 and !Right[i][j]) {
        uni.merge(i * W + j + 1, i * W + j);
      }
    }
  }
  for (int i = 0; i < H * W; i++) {
    if (uni.root(i) != i) continue;
    long long h = i / W;
    long long w = i % W;
  }
  for (int i = 0; i < H * W; i++) {
    long long h = i / W;
    long long w = i % W;
    cerr << h << " " << w << " ";
    h = uni.root(i) / W;
    w = uni.root(i) % W;
    cerr << h << " " << w << "\n";
  }
  return 0;
}
