#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}

/*
二次元グリッド、index番号で圧縮して1000*1000に。
BFS。

どう表記したらいいんだろう…
例１
-+-+-+-+-
#########-
#.......#+
#.S.###.#- 1　  S(2,2)はどう判定したものか
#...#.#.#+
#.###.###- 2
#.#...#.#+
#.#####.#-
#.......#+
#########-

(x, y) = [x][y]だとわかりにくい・・・
反転してやる。

要素数+2) * 2
#####ここまでDef
#....
#.@@@ほんちゃんのかべ
#.@

どう書き込もう…
set.find() - set.begin()
ってできないんだ。
配列にいれてsortしてlower_boundしかないかな

Aのマップはindexだけで書いていく。
描画きた
--0-1-2-3-4-5-6-7-8--
#####################-
#...................#-
#.....#.............#0
#.....#.............#-
#.....#.......#.....#1
#.....#.......#.....#-
#.#################.#2
#.....#.......#.....#-
#.....#.S.###.#.....#3
#.....#...#.#.#.....#-
#.....#.###.#####...#4
#.....#.#...#.#.....#-
#.....#.#####.#.....#5
#.....#.......#.....#-
#...#############...#6
#.....#.......#.....#-
#.....#.......#.....#7
#.............#.....#-
#.............#.....#8
#...................#-
#####################-

ともに-の箇所で通行可能なら面積換算。

2 2
-1 1 -1
-1 1 1
-1 -1 1
1 -1 1
開始位置バグ
ys:4 xs:4
0
--0-1--
#######
#.....#
#.###.#
#.#.#.#
#.###.#
#.....#
#######



ys:12 xs:28
0
ys:12 xs:28
0
#######################################
#.....................................#
#.......................#.............#
#.......................#.............#
#.........#.............#.............#
#.........#.............#.............#
#.........#.........#...#.............#
#.........#.........#...#.............#
#.........#.........#.#.#.............#
#.........#.........#.#.#.............#
#.........#...###################.....#
#.........#.........#.#.#..S..........#
#.........#.......#.#.#.#.............#
#.........#.......#.#.#.#.............#
#.........#.....#.#.#.#.#.............#
#.........#.....#.#.#.#.#.............#
#.....#######################.........#
#.........#.....#.#.#.#.#.............#
#.......###################...........#
#.........#.....#.#.#.#.#.............#
#.###################################.#
#.........#.....#.#.#.#.#.............#
#...###############################...#
#.........#.....#.#.#.#.#.............#
#.........#.....#.#.#.#.#.............#
#.........#.....#...#.#.#.............#
#.........#.....#...#.#.#.............#
#...............#...#.#.#.............#
#...............#...#.#.#.............#
#...................#.#.#.............#
#...................#.#.#.............#
#...................#...#.............#
#...........###################.......#
#...................#...#.............#
#...................#...#.............#
#...................#.................#
#...................#.................#
#.....................................#
#######################################



*/

#include <bits/stdc++.h>
using namespace std;
using ll=long long;

struct Z{
  ll y;
  ll x;
  ll depth;
};

ll N, M, y, x, depth, xs, ys, ans=0;
ll ysize, xsize;
vector<ll> y_vec={1,0,-1,0};
vector<ll> x_vec={0,1,0,-1};
vector<vector<ll>> A(6010, vector<ll> (6010, 0));
vector<ll> var_s(0, 0), hor_s(0, 0);
queue<Z> Q;

void bfs(){
  while(!Q.empty()){
    Z now=Q.front(); Q.pop();
    y=now.y, x=now.x, depth=now.depth;
    if (y == 1 || y == ysize - 2 || x == 1 || x == xsize - 2){
      ans = oo;
      return;
    }
    if ((y % 2) && (x % 2)){
      ll y1 = y/2;
      ll x1 = x/2;
      if (y1 && x1)
        ans += (var_s[y1] - var_s[y1-1]) * (hor_s[x1] - hor_s[x1-1]);
    }
    for(ll i=0; i<4; i++){
      Z next={y+y_vec[i], x+x_vec[i], depth+1};
//cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
      if (A[next.y][next.x] == -1) continue;
      A[next.y][next.x] = -1;
      Q.push(next);
//cout<<" d:"<<next.depth<<endl;          
   }
  }
}

//A[y][x], A(x,y)として管理
int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> N >> M;
  map<ll, ll> vm, hm;
  vector<pair<ll, ll>> var(N+1, {0, 0}), hor(M+1, {0, 0});
  vector<ll> varc(N+1, 0), hord(M+1, 0);
  rep(i, N){
    ll a, b, c; cin >> a >> b >> c;
    if (vm[a] == 0)
      var_s.push_back(a);
    if (vm[b] == 0)
      var_s.push_back(b);
    vm[a]++;
    vm[b]++;
    var[i] = {a, b};
    varc[i] = c;
    if (hm[c] == 0)
      hor_s.push_back(c);//縦線のx座標
    hm[c]++;
  }
  rep(i, M){
    ll d, e, f; cin >> d >> e >> f;
    if (hm[e] == 0)
      hor_s.push_back(e);
    if (hm[f] == 0)
      hor_s.push_back(f);
    hm[e]++;
    hm[f]++;
    hor[i] = {e, f};
    hord[i] = d;
    if (vm[d] == 0)
      var_s.push_back(d);//横線のy座標の記述
    vm[d]++;
  }
  ysize = var_s.size() * 2 + 3;
  xsize = hor_s.size() * 2 + 3;

  //A初期化
//  A.assign(ysize, vector<ll>(xsize, 0));
  for (ll i=0; i<ysize; i++){
    A[i][0] = -1;
    A[i][xsize-1] = -1;
  }
  for (ll j=0; j<xsize; j++){
    A[0][j] = -1;
    A[ysize-1][j] = -1;
  }

  //indexと対応
  vector<ll> yi(6010, 0);
  vector<ll> xi(6010, 0);
  sort(all(var_s));
  sort(all(hor_s));
  ll vsz = var_s.size(), hsz = hor_s.size();
  rep(i, vsz) yi[i] = var_s[i];
  rep(i, hsz) xi[i] = hor_s[i];

  //Aに縦線かいてく 2,4,6...
  rep(i, N){
    ll y1 = var[i].first;
    ll y2 = var[i].second;
    ll c = varc[i];
    ll yi1 = lower_bound(all(var_s), y1) - var_s.begin();
    ll yi2 = lower_bound(all(var_s), y2) - var_s.begin();
    ll ci = lower_bound(all(hor_s), c) - hor_s.begin();
    yi1 = (yi1 + 1) * 2;
    yi2 = (yi2 + 1) * 2;
    ci = (ci + 1) * 2;
    for(ll j = yi1; j <= yi2; j++)
      A[j][ci] = -1;
//cout<<"i:"<<i<<" yi1:"<<yi1<<" yi2:"<< yi2 << " ci:"<< ci << endl;    
  }
  //Aに横線
  rep(i, M){
    ll x1 = hor[i].first;
    ll x2 = hor[i].second;
    ll d = hord[i];
    ll xi1 = lower_bound(all(hor_s), x1) - hor_s.begin();
    ll xi2 = lower_bound(all(hor_s), x2) - hor_s.begin();
    ll di = lower_bound(all(var_s), d) - var_s.begin();
    xi1 = (xi1 + 1) * 2;
    xi2 = (xi2 + 1) * 2;
    di = (di + 1) * 2;
//cout<<"i:"<<i<<" xi1:"<<xi1<<" xi2:"<< xi2 << " di:"<< di << endl;    
    for(ll j = xi1; j <= xi2; j++)
      A[di][j] = -1;
  }

//開始位置
  ll ysi = lower_bound(all(var_s), 0) - var_s.begin();
  ll xsi = lower_bound(all(hor_s), 0) - hor_s.begin();
  ys = (ysi + 1) * 2;
  xs = (xsi + 1) * 2;
//cout << "ys:" << ys << " xs:" << xs << endl;
  if (ysi == var_s.size() || var_s[ysi] > 0)
    ys--;
  if (xsi == hor_s.size() || hor_s[xsi] > 0)
    xs--;
  A[ys][xs] = -1;

  Z start={ys, xs, 0};
  Q.emplace(start);
  bfs();
  if (ans==oo)
    cout << "INF" << endl;
  else
    cout<<ans<<endl;
/*rep(i, hor_s.size())
cout << hor_s[i]<<" ";
cout<<endl;
  
rep(i, var_s.size())
cout << var_s[i]<<" ";
cout<<endl;  
  
rep(i, ysize){
  rep(j, xsize){
    if (i==ys && j==xs)
      cout<<'S';
    else if (A[i][j] == -1)
      cout<<'#';
    else
      cout<<".";
  }
  cout<<endl;
}*/
}

Measured memory_bytes: 286472