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

from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    xn=len(dex)
    yn=len(dey)

    tt=[[-1]*yn*2 for _ in range(xn*2)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2+1):
            tt[i][coy[y]*2]=0
    for x0,x1,y in hl:
        for i in range(coy[x0]*2,coy[x1]*2+1):
            tt[cox[y]*2][i]=0

    stack=[(cox[0]*2,coy[0]*2)]
    tt[cox[0]*2][coy[0]*2]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=2*xn or nj>=2*yn:continue
            if tt[ni][nj]!=-1:continue
            if ni==0 or nj==0 or ni==2*xn-1 or nj==2*yn-1:
                print("INF")
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))

    ans=0
    for i in range(1,xn*2,2):
        for j in range(1,yn*2,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dex[j//2])
    print(ans)

main()