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 PyPy3 (7.3.0) (judge verdict: Time Limit Exceeded)

N,M = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N ]
DEF = [list(map(int,input().split())) for _ in [0]*M ]

X = [0]*3003
Y = [0]*3003
INF = 10**9 + 10
X[-1] = -INF
Y[-1] = -INF
X[-2] = INF
Y[-2] = INF
i = 0
j = 0
for a,b,c in ABC:
    X[i] = a
    X[i+1] = b
    Y[j] = c
    i += 2
    j += 1
for d,e,f in DEF:
    X[i] = d
    Y[j] = e
    Y[j+1] = f
    i += 1
    j += 2
X = sorted(set(X))
Y = sorted(set(Y))
Xdict = {x:i for i,x in enumerate(X)}
Ydict = {y:i for i,y in enumerate(Y)}

nx = len(X)
ny = len(Y)
nxy = nx * ny
E = {(x,y):{(X[i-1],y),(X[i+1],y),(x,Y[j-1]),(x,Y[j+1])} for i,x in enumerate(X[1:-1],1) for j,y in enumerate(Y[1:-1],1)}

x0 = X[0]
x1 = X[-1]
for y in Y:
    E[(x0,y)] = set()
    E[(x1,y)] = set()
y0 = Y[0]
y1 = Y[-1]
for x in X:
    E[(x,y0)] = set()
    E[(x,y1)] = set()

for a,b,c in ABC:
    y = Y[Ydict[c]-1]
    for x in X[Xdict[a]:Xdict[b]]:
        E[(x,c)].discard((x,y))
        E[(x,y)].discard((x,c))

for d,e,f in DEF:
    x = X[Xdict[d]-1]
    for y in Y[Ydict[e]:Ydict[f]]:
        E[(d,y)].discard((x,y))
        E[(x,y)].discard((d,y))

def dist_bfs(N,E,start):
    d = {start:0}
    q = [start]
    while q:
        qq = []
        for i in q:
            di = d[i]
            for j in E[i]:
                if j in d:continue
                d[j] = di+1
                q.append(j)
        q = qq
    return d

d = dist_bfs(nxy,E,(0,0))
burst = False
ans = 0

dX = {x:x2-x for x,x2 in zip(X[:-1],X[1:])}
dY = {y:y2-y for y,y2 in zip(Y[:-1],Y[1:])}

for x,y in d:
    if abs(x) == INF or abs(y) == INF:
        ans = "INF"
        break
    ans += dX[x]*dY[y]

print(ans)