Problem:
Score: 600 points

Problem Statement

There is a grass field that stretches infinitely.

In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0).

There are also N north-south lines and M east-west lines drawn on the field. The i-th north-south line is the segment connecting the points (A_i, C_i) and (B_i, C_i), and the j-th east-west line is the segment connecting the points (D_j, E_j) and (D_j, F_j).

What is the area of the region the cow can reach when it can move around as long as it does not cross the segments (including the endpoints)? If this area is infinite, print INF instead.

Constraints

All values in input are integers between -10^9 and 10^9 (inclusive).

1 \leq N, M \leq 1000

A_i < B_i\ (1 \leq i \leq N)

E_j < F_j\ (1 \leq j \leq M)

The point (0, 0) does not lie on any of the given segments.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
:
A_N B_N C_N
D_1 E_1 F_1
:
D_M E_M F_M

Output

If the area of the region the cow can reach is infinite, print INF; otherwise, print an integer representing the area in \mathrm{cm^2}.

(Under the constraints, it can be proved that the area of the region is always an integer if it is not infinite.)

Sample Input 1

5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4

Sample Output 1

13

The area of the region the cow can reach is 13\ \mathrm{cm^2}.

Sample Input 2

6 1
-3 -1 -2
-3 -1 1
-2 -1 2
1 4 -2
1 4 -1
1 4 1
3 1 4

Sample Output 2

INF

The area of the region the cow can reach is infinite.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
 
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;
 
#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
 
#define en "\n"
 
constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 1000000010;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
 
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
 
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; }
 
void remove_duplication(vector<int> &v){
    sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
}
 
ll ans = 0;
int H,W;
bool wall[3030][3030][4], vis[3030][3030];
VI h{0,INF,-INF},w{0,INF,-INF};
 
void Main(){
    int N,M; cin >> N >> M;
    VI A(N),B(N),C(N),D(M),E(M),F(M);
    REP(i,N){
        cin >> A[i] >> B[i] >> C[i];
        h.push_back(A[i]);
        h.push_back(B[i]);
        w.push_back(C[i]);
    }
    REP(i,M){
        cin >> D[i] >> E[i] >> F[i];
        h.push_back(D[i]);
        w.push_back(E[i]);
        w.push_back(F[i]);
    }
 
    remove_duplication(h); remove_duplication(w);
    H = h.size(); W = w.size();
  h.push_back(h[H-1]+1); w.push_back(w[W-1]+1);
    map<int,int> mh,mw;
    REP(i,H) mh[h[i]] = i;
    REP(i,W) mw[w[i]] = i;
 
    REP(i,N){
        int x1 = mh[A[i]], x2 = mh[B[i]], y = mw[C[i]];
        FOR(j,x1,x2) wall[j][y][2] = true;
        FOR(j,x1,x2) wall[j][y-1][3] = true;
    }
    REP(i,M){
        int x = mh[D[i]], y1 = mw[E[i]], y2 = mw[F[i]];
        FOR(j,y1,y2) wall[x][j][0] = true;
        FOR(j,y1,y2) wall[x-1][j][1] = true;
    }
 
    stack<P> s; s.emplace(mh[0],mw[0]);
    while(!s.empty()){
        P p = s.top(); s.pop();
        int i = p.first, j = p.second;
        if(i<0 || j<0 || i>=H || j>=W) continue;
        if(vis[i][j]) continue;
        vis[i][j] = true;
 
        ans += (ll)(h[i+1] - h[i]) * (w[j+1] - w[j]);
 
        if(wall[i][j][0]==false) s.emplace(i-1,j);
        if(wall[i][j][1]==false) s.emplace(i+1,j);
        if(wall[i][j][2]==false) s.emplace(i,j-1);
        if(wall[i][j][3]==false) s.emplace(i,j+1);
    }
 
    bool flag = false;
    REP(i,H) if(vis[i][0] || vis[i][W-1]) flag = true;
    REP(i,W) if(vis[0][i] || vis[H-1][i]) flag = true;
 
    if(flag) cout << "INF" << en;
    else cout << ans << en;
    return;
}
 
int main(void){
    cin.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    Main();
    return 0;
}