Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(q, enable, x_list, y_list, moves):
    c = q.pop()

    for dc, banned in moves:
        nc = c + dc
        if dc < 0:
            if banned[c]:
                continue
        else:
            if banned[nc]:
                continue
        if enable[nc] == -1:
            return -1
        if enable[nc] == 1:
            continue
        q.append(nc)
        enable[nc] = 1

    i, j = divmod(c, row)
    t = y_list[i - 1]
    b = y_list[i]
    l = x_list[j - 1]
    r = x_list[j]
    return (b - t) * (r - l)


@njit
def solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)
    enable[s] = 1
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    q = [s]
    ans = 0
    while q:
        ret = dfs(q, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    return ans


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

sys.setrecursionlimit(10 ** 7)

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(c, enable, x_list, y_list, moves):
    enable[c] = 1
    ans = 0

    for dc, banned in moves:
        nc = c + dc
        if dc < 0:
            if banned[c]:
                continue
        else:
            if banned[nc]:
                continue
        if enable[nc] == -1:
            return -1
        if enable[nc] == 1:
            continue
        ret = dfs(nc, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    i, j = divmod(c, row)
    t = y_list[i - 1]
    b = y_list[i]
    l = x_list[j - 1]
    r = x_list[j]
    ans += (b - t) * (r - l)

    return ans


@njit
def solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    return dfs(s, enable, x_list, y_list, moves)


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(c, enable, x_list, y_list, moves):
    enable[c] = 1
    ans = 0

    for dc, banned in moves:
        nc = c + dc
        if dc < 0:
            if banned[c]:
                continue
        else:
            if banned[nc]:
                continue
        if enable[nc] == -1:
            return -1
        if enable[nc] == 1:
            continue
        ret = dfs(nc, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    i, j = divmod(c, row)
    t = y_list[i - 1]
    b = y_list[i]
    l = x_list[j - 1]
    r = x_list[j]
    ans += (b - t) * (r - l)

    return ans


@njit
def solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    return dfs(s, enable, x_list, y_list, moves)


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    q = [s]
    banned_right = np.roll(banned_left, -1)
    banned_down = np.roll(banned_up, -row)
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    ans = 0
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1

        i, j = divmod(c, row)
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)

        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    return ans


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            enable[0] = -2
            return enable
        enable[c] = 1
        for dc, banned in moves:
            nc = c + dc
            if dc < 0:
                if banned[c]:
                    continue
            else:
                if banned[nc]:
                    continue
            if enable[nc] == 1:
                continue
            q.append(nc)
    return enable


@njit
def get_ans(enable, x_list, y_list):
    global col, row
    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

enable = dfs(x_dict, y_dict, banned_up, banned_left)
if enable[0] == -2:
    print('INF')
else:
    ans = get_ans(enable, x_list, y_list)
    print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            enable[0] = -2
            return enable
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)
    return enable


@njit
def get_ans(enable, x_list, y_list):
    global col, row
    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

enable = dfs(x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
if enable[0] == -2:
    print('INF')
else:
    ans = get_ans(enable, x_list, y_list)
    print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
ver_lines = list(zip(aaa, bbb, ccc))
hor_lines = list(zip(ddd, eee, fff))
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
ver_lines = list(zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]))
hor_lines = list(zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]))
x_list = {0}
y_list = {0}
for a, b, c in ver_lines:
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)

for d, e, f in hor_lines:
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(col):
        ri = i * row
        for j in range(row):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import List, Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())


@njit
def arrange_input(n, m, abcdef):
    ver_lines = List()
    hor_lines = List()
    x_list = set()
    y_list = set()
    n3 = n * 3
    for i in range(0, n3, 3):
        a = abcdef[i]
        b = abcdef[i + 1]
        c = abcdef[i + 2]
        y_list.add(a)
        y_list.add(b)
        x_list.add(c)
        ver_lines.append((a, b, c))
    for i in range(n3, n3 + m * 3, 3):
        d = abcdef[i]
        e = abcdef[i + 1]
        f = abcdef[i + 2]
        y_list.add(d)
        x_list.add(e)
        x_list.add(f)
        hor_lines.append((d, e, f))
    x_list.add(0)
    y_list.add(0)

    return ver_lines, hor_lines, x_list, y_list


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict, row):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict, row):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right, col, row):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right, col, row):
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(col):
        ri = i * row
        for j in range(row):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


@njit
def solve(n, m, abcdef):
    ver_lines, hor_lines, x_list, y_list = arrange_input(n, m, abcdef)
    x_list = np.array(sorted(x_list), dtype=np.int64)
    y_list = np.array(sorted(y_list), dtype=np.int64)
    x_dict = create_dict(x_list)
    y_dict = create_dict(y_list)
    row_real = len(x_list)
    col_real = len(y_list)
    row = row_real + 2
    col = col_real + 2
    total = row * col

    banned_up = np.zeros(total, dtype=np.int16)
    banned_down = np.zeros(total, dtype=np.int16)
    banned_left = np.zeros(total, dtype=np.int16)
    banned_right = np.zeros(total, dtype=np.int16)

    for a, b, c in ver_lines:
        register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict, row)
    for d, e, f in hor_lines:
        register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict, row)

    accumulate_banned(banned_up, banned_down, banned_left, banned_right, col, row)
    return dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right, col, row)


abcdef = np.array(abcdef, dtype=np.int64)
ans = solve(n, m, abcdef)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import List, Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())


@njit
def arrange_input(abcdef):
    ver_lines = List()
    hor_lines = List()
    x_list = set()
    y_list = set()
    n3 = n * 3
    for i in range(0, n3, 3):
        a = abcdef[i]
        b = abcdef[i + 1]
        c = abcdef[i + 2]
        y_list.add(a)
        y_list.add(b)
        x_list.add(c)
        ver_lines.append((a, b, c))
    for i in range(n3, n3 + m * 3, 3):
        d = abcdef[i]
        e = abcdef[i + 1]
        f = abcdef[i + 2]
        y_list.add(d)
        x_list.add(e)
        x_list.add(f)
        hor_lines.append((d, e, f))
    x_list.add(0)
    y_list.add(0)

    return ver_lines, hor_lines, x_list, y_list


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


abcdef = np.array(abcdef, dtype=np.int64)
ver_lines, hor_lines, x_list, y_list = arrange_input(abcdef)
x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(col):
        ri = i * row
        for j in range(row):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import List, Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())


@njit
def arrange_input(abcdef):
    ver_lines = List()
    hor_lines = List()
    x_list = set()
    y_list = set()
    n3 = n * 3
    for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
        y_list.add(a)
        y_list.add(b)
        x_list.add(c)
        ver_lines.append((a, b, c))

    for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
        y_list.add(d)
        x_list.add(e)
        x_list.add(f)
        hor_lines.append((d, e, f))
    x_list.add(0)
    y_list.add(0)

    x_list = np.array(sorted(x_list), dtype=np.int64)
    y_list = np.array(sorted(y_list), dtype=np.int64)

    return ver_lines, hor_lines, x_list, y_list


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


abcdef = np.array(abcdef, dtype=np.int64)
ver_lines, hor_lines, x_list, y_list = arrange_input(abcdef)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(col):
        ri = i * row
        for j in range(row):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
# x_dict = {x: i for i, x in enumerate(x_list, start=1)}
# y_dict = {y: i for i, y in enumerate(y_list, start=1)}
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(col):
        ri = i * row
        for j in range(row):
            if enable[ri + j] != 1:
                continue
            t = y_list[i - 1]
            b = y_list[i]
            l = x_list[j - 1]
            r = x_list[j]
            ans += (b - t) * (r - l)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
# x_dict = {x: i for i, x in enumerate(x_list, start=1)}
# y_dict = {y: i for i, y in enumerate(y_list, start=1)}
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)
banned_up = banned_up.tolist()
banned_down = banned_down.tolist()
banned_left = banned_left.tolist()
banned_right = banned_right.tolist()

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
from numba import njit

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)


@njit
def register_ver_lines(a, b, c):
    global banned_left, banned_right
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f):
    global banned_up, banned_down
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


for a, b, c in ver_lines:
    register_ver_lines(a, b, c)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f)

for i in range(1, col):
    ri0 = row * (i - 1)
    ri1 = row * i
    for j in range(1, row):
        banned_up[ri1 + j] += banned_up[ri1 + j - 1]
        banned_down[ri1 + j] += banned_down[ri1 + j - 1]
        banned_left[ri1 + j] += banned_left[ri0 + j]
        banned_right[ri1 + j] += banned_right[ri0 + j]

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for ri in range(ai * row, bi * row, row):
        walls[ri + j].add(ri + j - 1)
        walls[ri + j - 1].add(ri + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    ri0 = row * i
    ri1 = row * (i - 1)
    for j in range(ej, fj):
        walls[ri0 + j].add(ri1 + j)
        walls[ri1 + j].add(ri0 + j)

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for ri in range(0, row * col, row):
    enable[ri] = -1
    enable[ri + row - 1] = -1
ri_last = row * (col - 1)
for j in range(row):
    enable[j] = -1
    enable[ri_last + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for i in range(ai, bi):
        walls[row * i + j].add(row * i + j - 1)
        walls[row * i + j - 1].add(row * i + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    for j in range(ej, fj):
        walls[row * i + j].add(row * (i - 1) + j)
        walls[row * (i - 1) + j].add(row * i + j)

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for i in range(col):
    enable[row * i] = -1
    enable[row * (i + 1) - 1] = -1
for j in range(row):
    enable[j] = -1
    enable[row * (col - 1) + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for i in range(ai, bi):
        walls[row * i + j].add(row * i + j - 1)
        walls[row * i + j - 1].add(row * i + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    for j in range(ej, fj):
        walls[row * i + j].add(row * (i - 1) + j)
        walls[row * (i - 1) + j].add(row * i + j)

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for i in range(col):
    enable[row * i] = -1
    enable[row * (i + 1) - 1] = -1
for j in range(row):
    enable[j] = -1
    enable[row * (col - 1) + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)


Fixed submission:
import sys
# from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)
for i in range(col):
    ru = banned_up_ij[i]
    rd = banned_down_ij[i]
    ri = row * i
    banned_up[ri] = ru[0]
    banned_down[ri] = rd[0]
    for j in range(1, row):
        banned_up[ri + j] = banned_up[ri + j - 1] + ru[j]
        banned_down[ri + j] = banned_down[ri + j - 1] + rd[j]
for j in range(row):
    rl = banned_left_ij[j]
    rr = banned_right_ij[j]
    banned_left[j] = rl[0]
    banned_right[j] = rr[0]
    for i in range(1, col):
        ri0 = (i - 1) * row
        ri1 = i * row
        banned_left[ri1 + j] = banned_left[ri0 + j] + rl[i]
        banned_right[ri1 + j] = banned_right[ri0 + j] + rr[i]

# banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
# banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
# banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
# banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        if enable[ri + j] != 1:
            continue
        t = y_list[i - 1]
        b = y_list[i]
        l = x_list[j - 1]
        r = x_list[j]
        ans += (b - t) * (r - l)
print(ans)