Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
def hashh(x1,y1,x2,y2):
    if x1>x2:
        x1,y1,x2,y2=x2,y2,x1,y1
    if x1==x2 and y1>y2:
        x1,y1,x2,y2=x2,y2,x1,y1
    return (x1 << 48) + (y1 << 32) + (x2 << 16)  +y2
abc_l=[list(map(int,input().split()))for _ in range(n)]
def_l=[list(map(int,input().split()))for _ in range(m)]
x=[0]
y=[0]
for a,b,c in abc_l:
    x.append(a)
    x.append(b)
    y.append(c)
for d,e,f in def_l:
    x.append(d)
    y.append(e)
    y.append(f)
x=sorted(list(set(x)))
y=sorted(list(set(y)))
h=len(x)+1
w=len(y)+1
dicx={}
dicy={}
for i in range(len(x)):dicx[x[i]]=i
for i in range(len(y)):dicy[y[i]]=i
grid=[[0]*w for _ in range(h)]
dont=set()
for a,b,c in abc_l:
    for i in range(dicx[a],dicx[b]):
        dont.add(hashh(i+1,dicy[c],i+1,dicy[c]+1))
for d,e,f in def_l:
    for j in range(dicy[e],dicy[f]):
        dont.add(hashh(dicx[d],j+1,dicx[d]+1,j+1))
if dicx[0]==0 or dicx[0]==h-1 or dicy[0]==0 or dicy[0]==w-1:
    exit(print("INF"))
stack=[(dicx[0],dicy[0])]
grid[dicx[0]][dicy[0]]=1
ans=0
for sx,sy in stack:
    ans+=(x[sx]-x[sx-1])*(y[sy]-y[sy-1])
    for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
        tx=sx+dx
        ty=sy+dy
        if grid[tx][ty]:continue
        if hashh(sx,sy,tx,ty) in dont:continue
        if tx==0 or tx==h-1 or ty==0 or ty==w-1:
            exit(print("INF"))
        grid[tx][ty]=1
        stack.append((tx,ty))
print(ans)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
def hashh(x1,y1,x2,y2):
    if x1>x2:
        x1,y1,x2,y2=x2,y2,x1,y1
    if x1==x2 and y1>y2:
        x1,y1,x2,y2=x2,y2,x1,y1
    return (x1, y1, x2, y2)
abc_l=[list(map(int,input().split()))for _ in range(n)]
def_l=[list(map(int,input().split()))for _ in range(m)]
x=[0]
y=[0]
for a,b,c in abc_l:
    x.append(a)
    x.append(b)
    y.append(c)
for d,e,f in def_l:
    x.append(d)
    y.append(e)
    y.append(f)
x=sorted(list(set(x)))
y=sorted(list(set(y)))
h=len(x)+1
w=len(y)+1
dicx={}
dicy={}
for i in range(len(x)):dicx[x[i]]=i
for i in range(len(y)):dicy[y[i]]=i
grid=[[0]*w for _ in range(h)]
dont=set()
for a,b,c in abc_l:
    for i in range(dicx[a],dicx[b]):
        dont.add(hashh(i+1,dicy[c],i+1,dicy[c]+1))
for d,e,f in def_l:
    for j in range(dicy[e],dicy[f]):
        dont.add(hashh(dicx[d],j+1,dicx[d]+1,j+1))
if dicx[0]==0 or dicx[0]==h-1 or dicy[0]==0 or dicy[0]==w-1:
    exit(print("INF"))
stack=[(dicx[0],dicy[0])]
grid[dicx[0]][dicy[0]]=1
ans=0
for sx,sy in stack:
    ans+=(x[sx]-x[sx-1])*(y[sy]-y[sy-1])
    for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
        tx=sx+dx
        ty=sy+dy
        if grid[tx][ty]:continue
        if hashh(sx,sy,tx,ty) in dont:continue
        if tx==0 or tx==h-1 or ty==0 or ty==w-1:
            exit(print("INF"))
        grid[tx][ty]=1
        stack.append((tx,ty))
print(ans)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0, -10**9-2])
ys = set([0, -10**9-2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
xappend = stack_x.append
yappend = stack_y.append
xpop = stack_x.pop
ypop = stack_y.pop
while stack_x:
    x = xpop()
    y = ypop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        xappend(x+2)
        yappend(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        xappend(x)
        yappend(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        xappend(x-2)
        yappend(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        xappend(x)
        yappend(y-2)
        field[x][y-2] = False

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

    #print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = [-10**9-2] + sorted(xs)
scatter_y = [-10**9-2] + sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = False

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

    #print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0, -10**9-2])
ys = set([0, -10**9-2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
xappend = stack_x.append
yappend = stack_y.append
xpop = stack_x.pop
ypop = stack_y.pop
while stack_x:
    x = xpop()
    y = ypop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        xappend(x+2)
        yappend(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        xappend(x)
        yappend(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        xappend(x-2)
        yappend(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        xappend(x)
        yappend(y-2)
        field[x][y-2] = False

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

    #print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = [-10**9-2] + sorted(xs)
scatter_y = [-10**9-2] + sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = False

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

    #print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

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

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = deque([(sx, sy)])
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

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

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = deque([(sx, sy)])
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Runtime Error):
from numba import njit
import numpy as np
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

abcs = []
defs = []
for _ in range(N-2):
    a, b = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    c = random.randint(-10**9, 10**9)
    if a == b:
        abcs.append((a, b+1, c))
    else:
        abcs.append((a, b, c))

abcs += [(-10**9, 10**9, -10**9), (-10**9, 10**9, 10**9)]

for _ in range(M-2):
    e, f = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    d = random.randint(-10**9, 10**9)
    if e == f:
        defs.append((d, e, f+1))
    else:
        defs.append((d, e, f))
    
defs += [(-10**9, -10**9, 10**9), (10**9, -10**9, 10**9)]

#'''

xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x) + 1
y_size = 2 * len(scatter_y) + 1

field = np.zeros((x_size, y_size), np.int32)

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1

@njit
def imos(field):
    for x in range(x_size-1):
        for y in range(y_size-1):
            field[x][y+1] += field[x][y]

    for y in range(y_size-1):
        for x in range(x_size-1):
            field[x+1][y] += field[x][y]

    return field

field = imos(field)

area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = 1
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy] == 0:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = 1

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size or y == y_size:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

abcs = []
defs = []
for _ in range(N-2):
    a, b = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    c = random.randint(-10**9, 10**9)
    if a == b:
        abcs.append((a, b+1, c))
    else:
        abcs.append((a, b, c))

abcs += [(-10**9, 10**9, -10**9), (-10**9, 10**9, 10**9)]

for _ in range(M-2):
    e, f = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    d = random.randint(-10**9, 10**9)
    if e == f:
        defs.append((d, e, f+1))
    else:
        defs.append((d, e, f))
    
defs += [(-10**9, -10**9, 10**9), (10**9, -10**9, 10**9)]

#'''

xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

abcs = []
defs = []
for _ in range(N-2):
    a, b = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    c = random.randint(-10**9, 10**9)
    if a == b:
        abcs.append((a, b+1, c))
    else:
        abcs.append((a, b, c))

abcs += [(-10**9, 10**9, -10**9), (-10**9, 10**9, 10**9)]

for _ in range(M-2):
    e, f = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    d = random.randint(-10**9, 10**9)
    if e == f:
        defs.append((d, e, f+1))
    else:
        defs.append((d, e, f))
    
defs += [(-10**9, -10**9, 10**9), (10**9, -10**9, 10**9)]

#'''

xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)


Fixed submission:
import sys
from itertools import accumulate
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([-10**9-2, 0, 10**9+2])
ys = set([-10**9-2, 0, 10**9+2])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[0] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1


for x in range(x_size):
    field[x] = list(accumulate(field[x]))

for x in range(x_size-1):
    for y in range(y_size):
        field[x+1][y] += field[x][y]


#print(*[''.join(map(lambda c: str(c).center(3, ' '), row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = 1
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] == field[x+2][y] == 0:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = 1
    if field[x][y+1] == field[x][y+2] == 0:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = 1
    if field[x-1][y] == field[x-2][y] == 0:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = 1
    if field[x][y-1] == field[x][y-2] == 0:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = 1

    delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
    delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
    area += delta_x * delta_y

print(area)