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 Python (3.8.2) (judge verdict: Runtime Error)

from collections import deque
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = [0]*n
b = [0]*n
c = [0]*n
d = [0]*m
e = [0]*m
f = [0]*m

for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())
for i in range(m):
    d[i], e[i], f[i] = map(int, input().split())

x_items = list(set(a+b+d+[0]))
y_items = list(set(c+e+f+[0]))

x_items.sort()
y_items.sort()
xi = 0
yi = 0
x_map = {}
y_map = {}
for i in x_items:
    x_map[i] = xi
    xi = xi + 1
for i in y_items:
    y_map[i] = yi
    yi = yi + 1


h = len(x_map)*2
w = len(y_map)*2
grid = [[0] * w for i in range(h)]
for i in range(n):
    a_comp = x_map[a[i]]*2
    b_comp = x_map[b[i]]*2
    c_comp = y_map[c[i]]*2
    for x in range(a_comp,b_comp+1):
        grid[x][c_comp] = 2

for i in range(m):
    d_comp = x_map[d[i]]*2
    e_comp = y_map[e[i]]*2
    f_comp = y_map[f[i]]*2
    for y in range(e_comp,f_comp+1):
        grid[d_comp][y] = 2

q = deque()
xo = x_map[0]*2
yo = y_map[0]*2
dx = (-1, 0, 1, 0)
dy = (0,-1,0,1)
q.append((xo,yo))
flug1 = True

grid[xo][yo] = 1
#xx = x_items[xo//2+1] - x_items[xo//2]
#yy = y_items[yo//2+1] - y_items[yo//2]
#ans = xx*yy
ans = 0
while q:
    x, y = q.popleft()
    if x%2 != 0 and y%2 != 0:
        xx = x_items[x//2+1] - x_items[x//2]
        yy = y_items[y//2+1] - y_items[y//2]
        ans = ans + xx*yy
    for i in range(4): 
        nx = x+dx[i]
        ny = y+dy[i]
        if nx < 0 or nx >= h:
            flug1 = False
            break
        if ny < 0 or ny >= w:
            flug1 = False
            break
        if grid[x+dx[i]][y+dy[i]] == 0:
            grid[x+dx[i]][y+dy[i]] = 1
            q.append((x+dx[i],y+dy[i]))
    if flug1 == False:
        ans = "INF"
        break

print(ans)