import sys

import numpy as np

from numba import njit



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



INF = 10**9 + 1



N, M = list(map(int, readline().split()))

data = np.int64(read().split()).reshape(-1, 3)



A = data[:N, 0]

B = data[:N, 1]

C = data[:N, 2]

D = data[N:, 0]

E = data[N:, 1]

F = data[N:, 2]



X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))

Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))

DX = X[1:] - X[:-1]

DY = Y[1:] - Y[:-1]



A, B, D = [np.searchsorted(X, x) for x in (A, B, D)]

C, E, F = [np.searchsorted(Y, x) for x in (C, E, F)]



H, W = len(X), len(Y)

N = H * W



@njit

def solve():

    def set_ng():

        ng = np.zeros((N, 4), np.bool_)



        for i in range(len(A)):

            for x in range(A[i], B[i]):

                v = x * W + C[i]

                ng[v][1] = 1

                ng[v - 1][0] = 1



        for i in range(len(D)):

            for y in range(E[i], F[i]):

                v = D[i] * W + y

                ng[v][3] = 1

                ng[v - W][2] = 1

        return ng



    ng = set_ng(A, B, C, D, E, F)

    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)

    v0 = x0 * W + y0

    visited = np.zeros(N, np.bool_)

    visited[v0] = 1

    stack = np.empty(N, np.int32)

    p = 0

    ret = 0



    def area(x):

        x, y = divmod(x, W)

        return DX[x] * DY[y]



    def push(x):

        nonlocal p, ret

        stack[p] = x

        visited[x] = 1

        ret += area(x)

        p += 1



    def pop():

        nonlocal p

        p -= 1

        return stack[p]



    push(v0)

    move = (1, -1, W, -W)

    while p:

        v = pop()

        for i in range(4):

            if ng[v][i]:

                continue

            w = v + move[i]

            if visited[w]:

                continue

            x, y = divmod(w, W)

            if x == 0 or x == H - 1 or y == 0 or y == W - 1:

                return 0

            push(w)

    return ret



x = solve()

if x == 0:

    print('INF')

else:

    print(x)
import sys

import numpy as np

from numba import njit



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



INF = 10**9 + 1



def precompute(N, M, data):

    data = data.reshape(-1, 3)

    A, B, C = data[:N].T

    D, E, F = data[N:].T

    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))

    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))

    DX, DY = np.diff(X), np.diff(Y)

    A = np.searchsorted(X, A)

    B = np.searchsorted(X, B)

    D = np.searchsorted(X, D)

    C = np.searchsorted(Y, C)

    E = np.searchsorted(Y, E)

    F = np.searchsorted(Y, F)

    return A, B, C, D, E, F, X, Y, DX, DY



@njit

def set_ng(A, B, C, D, E, F, H, W):

    ng = np.zeros((H * W, 4), np.bool_)



    for i in range(len(A)):

        for x in range(A[i], B[i]):

            v = x * W + C[i]

            ng[v][1] = 1

            ng[v - 1][0] = 1



    for i in range(len(D)):

        for y in range(E[i], F[i]):

            v = D[i] * W + y

            ng[v][3] = 1

            ng[v - W][2] = 1

    return ng



@njit

def solve(A, B, C, D, E, F, X, Y, DX, DY):

    H, W = len(X), len(Y)

    N = H * W

    ng = set_ng(A, B, C, D, E, F, H, W)

    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)

    v0 = x0 * W + y0

    visited = np.zeros(N, np.bool_)

    visited[v0] = 1

    stack = np.empty(N, np.int32)

    p = 0

    ret = 0



    def area(x):

        x, y = divmod(x, W)

        return DX[x] * DY[y]



    def push(x):

        nonlocal p, ret

        stack[p] = x

        visited[x] = 1

        ret += area(x)

        p += 1



    def pop():

        nonlocal p

        p -= 1

        return stack[p]



    push(v0)

    move = (1, -1, W, -W)

    while p:

        v = pop()

        for i in range(4):

            if ng[v][i]:

                continue

            w = v + move[i]

            if visited[w]:

                continue

            x, y = divmod(w, W)

            if x == 0 or x == H - 1 or y == 0 or y == W - 1:

                return 0

            push(w)

    return ret



N, M = list(map(int, readline().split()))

data = np.int64(read().split())



x = solve(*precompute(N, M, data))

if x == 0:

    print('INF')

else:

    print(x)
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
INF = 10**9 + 1
N, M = list(map(int, readline().split()))
data = np.int64(read().split()).reshape(-1, 3)
A = data[:N, 0]
B = data[:N, 1]
C = data[:N, 2]
D = data[N:, 0]
E = data[N:, 1]
F = data[N:, 2]
X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]
A, B, D = [np.searchsorted(X, x) for x in (A, B, D)]
C, E, F = [np.searchsorted(Y, x) for x in (C, E, F)]
H, W = len(X), len(Y)
N = H * W


@njit
def solve():
    def set_ng():
        ng = np.zeros((N, 4), np.bool_)
        for i in range(len(A)):
            for x in range(A[i], B[i]):
                v = x * W + C[i]
                ng[v][1] = 1
                ng[v - 1][0] = 1
        for i in range(len(D)):
            for y in range(E[i], F[i]):
                v = D[i] * W + y
                ng[v][3] = 1
                ng[v - W][2] = 1
        return ng

    ng = set_ng(A, B, C, D, E, F)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(v0)
    move = (1, -1, W, -W)
    while p:
        v = pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret


x = solve()
if x == 0:
    print("INF")
else:
    print(x)

import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
INF = 10**9 + 1


def precompute(N, M, data):
    data = data.reshape(-1, 3)
    A, B, C = data[:N].T
    D, E, F = data[N:].T
    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
    DX, DY = np.diff(X), np.diff(Y)
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B)
    D = np.searchsorted(X, D)
    C = np.searchsorted(Y, C)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F)
    return A, B, C, D, E, F, X, Y, DX, DY


@njit
def set_ng(A, B, C, D, E, F, H, W):
    ng = np.zeros((H * W, 4), np.bool_)
    for i in range(len(A)):
        for x in range(A[i], B[i]):
            v = x * W + C[i]
            ng[v][1] = 1
            ng[v - 1][0] = 1
    for i in range(len(D)):
        for y in range(E[i], F[i]):
            v = D[i] * W + y
            ng[v][3] = 1
            ng[v - W][2] = 1
    return ng


@njit
def solve(A, B, C, D, E, F, X, Y, DX, DY):
    H, W = len(X), len(Y)
    N = H * W
    ng = set_ng(A, B, C, D, E, F, H, W)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(v0)
    move = (1, -1, W, -W)
    while p:
        v = pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret


N, M = list(map(int, readline().split()))
data = np.int64(read().split())
x = solve(*precompute(N, M, data))
if x == 0:
    print("INF")
else:
    print(x)

-N, M = list(map(int, readline().split())) -data = np.int64(read().split()).reshape(-1, 3) -A = data[:N, 0] -B = data[:N, 1] -C = data[:N, 2] -D = data[N:, 0] -E = data[N:, 1] -F = data[N:, 2] -X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]])) -Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]])) -DX = X[1:] - X[:-1] -DY = Y[1:] - Y[:-1] -A, B, D = [np.searchsorted(X, x) for x in (A, B, D)] -C, E, F = [np.searchsorted(Y, x) for x in (C, E, F)] -H, W = len(X), len(Y) -N = H * W + + +def precompute(N, M, data): +    data = data.reshape(-1, 3) +    A, B, C = data[:N].T +    D, E, F = data[N:].T +    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]])) +    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]])) +    DX, DY = np.diff(X), np.diff(Y) +    A = np.searchsorted(X, A) +    B = np.searchsorted(X, B) +    D = np.searchsorted(X, D) +    C = np.searchsorted(Y, C) +    E = np.searchsorted(Y, E) +    F = np.searchsorted(Y, F) +    return A, B, C, D, E, F, X, Y, DX, DY -def solve(): -    def set_ng(): -        ng = np.zeros((N, 4), np.bool_) -        for i in range(len(A)): -            for x in range(A[i], B[i]): -                v = x * W + C[i] -                ng[v][1] = 1 -                ng[v - 1][0] = 1 -        for i in range(len(D)): -            for y in range(E[i], F[i]): -                v = D[i] * W + y -                ng[v][3] = 1 -                ng[v - W][2] = 1 -        return ng +def set_ng(A, B, C, D, E, F, H, W): +    ng = np.zeros((H * W, 4), np.bool_) +    for i in range(len(A)): +        for x in range(A[i], B[i]): +            v = x * W + C[i] +            ng[v][1] = 1 +            ng[v - 1][0] = 1 +    for i in range(len(D)): +        for y in range(E[i], F[i]): +            v = D[i] * W + y +            ng[v][3] = 1 +            ng[v - W][2] = 1 +    return ng -    ng = set_ng(A, B, C, D, E, F) + +@njit +def solve(A, B, C, D, E, F, X, Y, DX, DY): +    H, W = len(X), len(Y) +    N = H * W +    ng = set_ng(A, B, C, D, E, F, H, W) -x = solve() +N, M = list(map(int, readline().split())) +data = np.int64(read().split()) +x = solve(*precompute(N, M, data))