Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from collections import deque
from bisect import bisect_left

def main():
    n,m=map(int,input().split())
    X,Y=set(),set()
    H=[]
    V=[]
    for _ in range(n):
        a,b,c=map(int,input().split())
        X.add(a); X.add(b)
        Y.add(c)
        V.append((a,b,c))
    for _ in range(m):
        d,e,f=map(int,input().split())
        X.add(d)
        Y.add(e); Y.add(f)
        H.append((d,e,f))
    X=list(X); Y=list(Y)
    X.sort(); Y.sort()
    MapX={}; MapY={}
    for i,x in enumerate(X):
        MapX[x]=i
    for i,y in enumerate(Y):
        MapY[y]=i
    h=len(MapX)*2; w=len(MapY)*2
    Grid=[[False]*w for _ in range(h)]
    for a,b,c in V:
        a=MapX[a]*2
        b=MapX[b]*2
        c=MapY[c]*2
        for x in range(a,b+1):
            Grid[x][c]=True
    for d,e,f in H:
        d=MapX[d]*2
        e=MapY[e]*2
        f=MapY[f]*2
        for y in range(e,f+1):
            Grid[d][y]=True
    s_x=bisect_left(X,0)*2-1
    s_y=bisect_left(Y,0)*2-1
    if s_x<0 or s_y<0 or s_x>=h-1 or s_y>=w-1:
        return 'INF'
    Grid[s_x][s_y]=True
    D=[(1,0),(-1,0),(0,1),(0,-1)]
    ans=0
    q=deque([(s_x,s_y)])
    Grid[s_x][s_y]=True
    while q:
        x,y=q.popleft()
        ans+=(X[x//2+1]-X[x//2])*(Y[y//2+1]-Y[y//2])
        for dx,dy in D:
            half_x,half_y=x+dx,y+dy
            if Grid[half_x][half_y]:
                continue
            to_x,to_y=half_x+dx,half_y+dy
            if to_x<0 or to_y<0 or to_x==h-1 or to_y==w-1:
                return 'INF'
            if Grid[to_x][to_y]:
                continue
            Grid[to_x][to_y]=True
            q.append((to_x,to_y))
    return ans
    
if __name__ == "__main__":
    print(main())

Fixed submission:
import sys
input=sys.stdin.readline
from collections import deque
from bisect import bisect_left

def main():
    n,m=map(int,input().split())
    X,Y=set(),set()
    H=[]
    V=[]
    for _ in range(n):
        a,b,c=map(int,input().split())
        X.add(a); X.add(b)
        Y.add(c)
        V.append((a,b,c))
    for _ in range(m):
        d,e,f=map(int,input().split())
        X.add(d)
        Y.add(e); Y.add(f)
        H.append((d,e,f))
    X=list(X); Y=list(Y)
    X.sort(); Y.sort()
    MapX={}; MapY={}
    for i,x in enumerate(X):
        MapX[x]=i
    for i,y in enumerate(Y):
        MapY[y]=i
    h=len(MapX)*2; w=len(MapY)*2
    Grid=[[0]*w for _ in range(h)]
    for a,b,c in V:
        a=MapX[a]*2; b=MapX[b]*2; c=MapY[c]*2
        for x in range(a,b+1):
            Grid[x][c]=1
    for d,e,f in H:
        d=MapX[d]*2; e=MapY[e]*2; f=MapY[f]*2
        for y in range(e,f+1):
            Grid[d][y]=1
    s_x=bisect_left(X,0)*2-1
    s_y=bisect_left(Y,0)*2-1
    if s_x<0 or s_y<0 or s_x>=h-1 or s_y>=w-1:
        return 'INF'
    Grid[s_x][s_y]=1
    D=[(1,0),(-1,0),(0,1),(0,-1)]
    ans=0
    q=deque([(s_x,s_y)])
    Grid[s_x][s_y]=1
    while q:
        x,y=q.popleft()
        ans+=(X[x//2+1]-X[x//2])*(Y[y//2+1]-Y[y//2])
        for dx,dy in D:
            half_x,half_y=x+dx,y+dy
            if Grid[half_x][half_y]!=0:
                continue
            to_x,to_y=half_x+dx,half_y+dy
            if to_x<0 or to_y<0 or to_x==h-1 or to_y==w-1:
                return 'INF'
            if Grid[to_x][to_y]!=0:
                continue
            Grid[to_x][to_y]=1
            q.append((to_x,to_y))
    return ans
    
if __name__=='__main__':
    print(main())