#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const long long mo = 1e9 + 7;   int n, m, k, nn;   template <typename T>   inline void read(T &X) {     X = 0;     int w = 0;     char ch = 0;     while (!isdigit(ch)) {       w |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();     if (w) X = -X;   }   vector<pair<int, int>> vc[3], vv[3], ys[maxn * 5];   int sum[(maxn * 5) << 2], fg[(maxn * 5) << 2];   void build(int rt, int l, int r, int v) {     sum[rt] = fg[rt] = v;     if (l == r) {       return;     }     int mid = (l + r) >> 1;     build((rt << 1), l, mid, v);     build((rt << 1 | 1), mid + 1, r, v);   }   void pu(int rt) { sum[rt] = sum[(rt << 1)] + sum[(rt << 1 | 1)]; }   void pd(int rt) {     if (fg[rt]) {       sum[(rt << 1)] += fg[rt];       sum[(rt << 1 | 1)] += fg[rt];       fg[(rt << 1)] += fg[rt];       fg[(rt << 1 | 1)] += fg[rt];       fg[rt] = 0;     }   }   void pdd(int rt) {     if (fg[rt]) {       sum[(rt << 1)] = sum[rt];       sum[(rt << 1 | 1)] = sum[rt];       fg[(rt << 1)] = fg[(rt << 1 | 1)] = 1;       fg[rt] = 0;     }   }   void update(int rt, int l, int r, int ql, int qr, int v) {     if (ql <= l && r <= qr) {       sum[rt] = v;       fg[rt] = 1;       return;     }     pdd(rt);     int mid = (l + r) >> 1;     if (ql <= mid) update((rt << 1), l, mid, ql, qr, v);     if (qr > mid) update((rt << 1 | 1), mid + 1, r, ql, qr, v);   }   void add(int rt, int l, int r, int p, int v) {     if (p <= l && r <= p) {       sum[rt] += v;       fg[rt] += v;       return;     }     pd(rt);     int mid = (l + r) >> 1;     if (p <= mid) add((rt << 1), l, mid, p, v);     if (p > mid) add((rt << 1 | 1), mid + 1, r, p, v);     pu(rt);   }   int query1(int rt, int l, int r, int q) {     if (q <= l && r <= q) {       return sum[rt];     }     pdd(rt);     int mid = (l + r) >> 1;     if (q <= mid) return query1((rt << 1), l, mid, q);     if (q > mid) return query1((rt << 1 | 1), mid + 1, r, q);   }   long long query2(int rt, int l, int r, int ql, int qr) {     if (ql <= l && r <= qr) {       return sum[rt];     }     pd(rt);     int mid = (l + r) >> 1;     long long ans = 0;     if (ql <= mid) ans = query2((rt << 1), l, mid, ql, qr);     if (qr > mid) ans += query2((rt << 1 | 1), mid + 1, r, ql, qr);     return ans;   }   void solve() {     nn = 1000000;     read(n);     read(m);     for (int i = (1); i <= (n); i++) {       int y, lx, rx;       read(y);       read(lx);       read(rx);       if (lx == 0)         vc[0].push_back({rx, y});       else         vc[1].push_back({lx, y});     }     for (int i = (1); i <= (m); i++) {       int x, ly, ry;       read(x);       read(ly);       read(ry);       if (ly == 0)         vv[0].push_back({x, ry});       else         vv[1].push_back({x, ly});     }     for (int i = (0); i <= (1); i++) {       if (!vc[i].empty()) sort(vc[i].begin(), vc[i].end());       if (!vv[i].empty()) sort(vv[i].begin(), vv[i].end());     }     reverse(vc[1].begin(), vc[1].end());     int sz0 = vv[0].size() - 1;     int sz1 = vv[1].size() - 1;     build(1, 0, nn, 0);     for (auto i : vc[1]) {       int fg = 1;       while (fg) {         fg = 0;         if (sz1 >= 0 && sz0 >= 0 && vv[0][sz0].first < vv[1][sz1].first &&             vv[1][sz1].first >= i.first) {           update(1, 0, nn, vv[1][sz1].second, nn, vv[1][sz1].first);           sz1--;           fg = 1;         } else if (sz0 < 0 && sz1 >= 0 && vv[1][sz1].first >= i.first) {           update(1, 0, nn, vv[1][sz1].second, nn, vv[1][sz1].first);           sz1--;           fg = 1;         } else if (sz1 >= 0 && sz0 >= 0 && vv[0][sz0].first > vv[1][sz1].first &&                    vv[0][sz0].first >= i.first) {           update(1, 0, nn, 0, vv[0][sz0].second, vv[0][sz0].first);           sz0--;           fg = 1;         } else if (sz1 < 0 && sz0 >= 0 && vv[0][sz0].first >= i.first) {           update(1, 0, nn, 0, vv[0][sz0].second, vv[0][sz0].first);           sz0--;           fg = 1;         }       }       int pos = query1(1, 0, nn, i.second);       ys[pos].push_back(i);     }     build(1, 0, nn, 0);     int j = 0, k = 0;     vv[0].push_back({nn, nn});     int nj = vv[0].size(), nk = vv[1].size();     for (auto i : vc[0]) {       add(1, 0, nn, i.second, 1);     }     int i = 0, ni = vc[0].size();     long long ans = 0;     while (j < nj || k < nk) {       pair<int, int> p;       if (j == nj)         p = vv[1][k++];       else if (k == nk)         p = vv[0][j++];       else if (vv[0][j].first < vv[1][k].first)         p = vv[0][j++];       else         p = vv[1][k++];       while (i < ni && vc[0][i].first < p.first) {         add(1, 0, nn, vc[0][i].second, -1);         i++;       }       if (k && p == vv[1][k - 1])         ans += query2(1, 0, nn, p.second, nn);       else         ans += query2(1, 0, nn, 0, p.second);       if (p.second == nn) ans++;       for (auto s : ys[p.first]) add(1, 0, nn, s.second, 1);     }     printf( %lld n , ans);   }   int main() {     int T = 1, cas = 1;     while (T--) {       solve();     }     return 0;   }