#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
  vector<long long> BIT;
  long long N;
  Fenwick(long long n) {
    N = n + 2;
    BIT.resize(N + 10);
  }
  void reset() {
    for (long long i = 0; i <= N; i++) {
      BIT[i] = 0;
    }
  }
  void add(long long r, long long value) {
    for (long long i = r; i <= N; i = i | (i + 1)) {
      BIT[i] += value;
    }
  }
  long long qry(long long r) {
    long long ans = 0;
    for (long long i = r; i >= 0; i = (i & (i + 1)) - 1) {
      ans += BIT[i];
    }
    return ans;
  }
};
const long long N = (long long)1e6;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<array<long long, 3>> x(n), y(m);
  for (auto &i : x) {
    cin >> i[0] >> i[1] >> i[2];
  }
  for (auto &i : y) {
    cin >> i[0] >> i[1] >> i[2];
  }
  m += 1;
  y.push_back({N, 0, N});
  long long ans = 0;
  Fenwick sg(N + 10);
  sg.add(N, 1);
  sg.add(0 * 1LL, 1);
  sort(y.begin(), y.end());
  vector<vector<long long>> add(N + 10), remove(N + 10);
  for (auto &i : x) {
    if (i[1] == 0) {
      add[0].push_back(i[0]);
      remove[i[2]].push_back(i[0]);
    } else {
      add[i[1]].push_back(i[0]);
      remove[N - 1].push_back(i[0]);
    }
  }
  long long cur = 0;
  42;
  for (long long i = 0; i <= N; i++) {
    for (auto &j : add[i]) {
      sg.add(j, 1);
    }
    while (cur < m and y[cur][0] == i) {
      long long u = max(y[cur][1], y[cur][2]);
      long long v = min(y[cur][1], y[cur][2]);
      if (v == 0) {
        ans += (sg.qry(u) - 1);
      } else {
        ans += (sg.qry(u) - sg.qry(v - 1) - 1);
      }
      cur += 1;
    }
    for (auto &j : remove[i]) {
      sg.add(j, -1);
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
