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: Runtime Error)

// solution for ABC168-F
// author : lynmisakura

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

#define REP(i,N) for(int i = 0;i < N;i++)
using ll = long long;

#define pb push_back
#define all(x) (x).begin(),(x).end()

int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};

#define maxn 2020
ll area[maxn][maxn];
int ng[maxn][maxn][4];
int used[maxn][maxn];

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n,m;cin >> n >> m;
    vector<int> a(n),b(n),c(n),d(m),e(m),f(m),x,y;
    
    x.pb(0);
    y.pb(0);
    
    REP(i,n){
        cin >> a[i] >> b[i] >> c[i];
        x.pb(a[i]);
        x.pb(b[i]);
        y.pb(c[i]);
    }
    REP(i,m){
        cin >> d[i] >> e[i] >> f[i];
        x.pb(d[i]);
        y.pb(e[i]);
        y.pb(f[i]);
    }
    
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)),x.end());
    y.erase(unique(all(y)),y.end());
    
    int sx = x.size(),sy = y.size();
    map<int,int> get_x,get_y;
    
    REP(i,sx){
        get_x[x[i]] = i;    
    }
    REP(i,sy){
        get_y[y[i]] = i;
    }
    
    // set area
    REP(i,sx - 1)REP(j,sy - 1) area[i][j] = (ll)(x[i+1] - x[i]) * (ll)(y[i+1] - y[i]);
    
    // set ng
    REP(i,n){
        
        int yid = get_y[c[i]];
        int l = get_x[a[i]],r = get_x[b[i]];
        
        for(int xid = l;xid < r;xid++){
            if(yid > 0){
                ng[xid][yid - 1][3] = 1;
            }    
            if(yid < sy - 1){
                ng[xid][yid][1] = 1;
            }
        }
        
    }
    REP(i,m){
    
        int xid = get_x[d[i]];
        int l = get_y[e[i]],r = get_y[f[i]];
        
        for(int yid = l;yid < r;yid++){
            if(xid > 0){
                ng[xid - 1][yid][0] = 1;
            }
            if(xid < sx - 1){
                ng[xid][yid][2] = 1;
            }
        }
        
    }
    
    ll ans = 0;
    int cnt = 0;
    {
        int x = get_x[0],y = get_y[0];
        //ans += area[x][y];
        
        queue<pair<int,int>> Q;
        Q.emplace(x,y);
        
        while(Q.size()){
            
            cnt++;
            auto e = Q.front();Q.pop();
            x = e.first,y = e.second;
            ans += area[x][y];
           // cout << area[x][y] << ' ' << cnt << ' ' << ans << '\n';
            used[x][y] = 1;
            
            //cout << x << ' ' << y << ' ' << area[x][y] << '\n';
            
            REP(dir,4){
                
                int nx = x + dx[dir];
                int ny = y + dy[dir];
            
                if(!ng[x][y][dir]){
                    
                    auto inner = [=](int x,int y){
                        return x >= 0 && x < sx - 1 && y >= 0 && y < sy - 1;
                    };
                    
                    if(!inner(nx,ny)){
                    
                        cout << "INF" << '\n';
                        return 0;
                        
                    }else{
                    
                        if(!used[nx][ny]) Q.emplace(nx,ny);
                        
                    }
                
                }
            
            }
            
        }
        
    }
    
   // cout << "cnt : " << cnt << '\n';
    
    cout << ans - 1 << '\n';
    return 0;
    
}