Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Any, Callable, Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a, i: a,
	lambda a, i: a[i[0]],
	lambda a, i: a[i[0]][i[1]],
	lambda a, i: a[i[0]][i[1]][i[2]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]],
)
ndinitializor = (
	lambda s, v: [v] * s,
	lambda s, v: [v] * s[0],
	lambda s, v: [[v] * s[1] for _ in range(s[0])],
	lambda s, v: [[[v] * s[2] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[v] * s[3] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[v] * s[4] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[[v] * s[5] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
)
ndshapegetter = (
	lambda a: len(a),
	lambda a: (len(a),),
	lambda a: (len(a), len(a[0])),
	lambda a: (len(a), len(a[0]), len(a[0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0]), len(a[0][0][0][0][0])),
)
nditerator = (
	lambda s: s,
	lambda s: ((i,) for i in range(s[0])),
	lambda s: ((i, j) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m, n) for n in range(s[5]) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
)

class nd (object):
	__slots__ = [
		'_data',
		'shape',
		'_getter_getter',
		'_setter_getter',
		'_iterator',
		'_initializer'
	]
	
	def __init__(self, data : list, shape : Tuple[int]):
		self._data = data
		self.shape = shape
		self._getter_getter = ndgetter[len(self.shape)]
		self._setter_getter = ndgetter[len(self.shape) - 1]
		self._iterator = nditerator[len(self.shape)]
		self._initializer = ndinitializor[len(self.shape)]
	
	def __len__(self):
		return len(self._data)
	
	def __getitem__(self, key):
		return self._data[key]
	
	def __setitem__(self, key, value):
		self._data[key] = value
	
	def __iter__(self):
		cls = type(self)
		sub_shape = self.shape[1:]
		if 0 < len(sub_shape):
			return (cls(v, sub_shape) for v in self._data)
		else:
			return iter(self._data)
	
	def at(self, indices : Tuple[int]):
		return self._getter_getter(self._data, indices)
	
	def set_at(self, indices : Tuple[int], value):
		self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def fill(self, value):
		for indices in self._iterator(self.shape):
			self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def apply_indices_inplace(self, func : Callable[[Any, Tuple[int]], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]], indices)
	def apply_inplace(self, func : Callable[[Any], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]])
	def apply_indices(self, func : Callable[[Any, Tuple[int]], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices), indices)
		return nd(new_data, self.shape)
	def apply(self, func : Callable[[Any], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices))
		return nd(new_data, self.shape)
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(element, other))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(other, element))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(element, other))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(other, element))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(ndinitializor[0](shape, fill_value), (shape,))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		return cls(ndinitializor[len(shape)](shape, fill_value), shape)
	
	@classmethod
	def list_fromiter(cls, iterable : Iterable, ndim=1):
		if ndim == 1:
			return list(iterable)
		else:
			return list(map(cls.list_fromiter, iterable))
	
	@classmethod
	def fromiter(cls, iterable : Iterable, ndim=1):
		new_data = cls.list_fromiter(iterable, ndim)
		return cls(new_data, ndshapegetter[ndim](new_data))
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a._data
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(*left_args, v, *right_args, **left_kwargs, **right_kwargs))
		return nd_func
	
	@classmethod
	def sorted(cls, iterable : Iterable):
		new_data = sorted(iterable)
		return cls(new_data, (len(new_data),))


def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = nd.sorted(set(C))
ys = nd.sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Any, Callable, Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a, i: a,
	lambda a, i: a[i[0]],
	lambda a, i: a[i[0]][i[1]],
	lambda a, i: a[i[0]][i[1]][i[2]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]],
)
ndinitializor = (
	lambda s, v: [v] * s,
	lambda s, v: [v] * s[0],
	lambda s, v: [[v] * s[1] for _ in range(s[0])],
	lambda s, v: [[[v] * s[2] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[v] * s[3] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[v] * s[4] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[[v] * s[5] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
)
ndshapegetter = (
	lambda a: len(a),
	lambda a: (len(a),),
	lambda a: (len(a), len(a[0])),
	lambda a: (len(a), len(a[0]), len(a[0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0]), len(a[0][0][0][0][0])),
)
nditerator = (
	lambda s: s,
	lambda s: ((i,) for i in range(s[0])),
	lambda s: ((i, j) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m, n) for n in range(s[5]) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
)

class nd (object):
	__slots__ = [
		'_data',
		'shape',
		'_getter_getter',
		'_setter_getter',
		'_iterator',
		'_initializer'
	]
	
	def __init__(self, data : list, shape : Tuple[int]):
		self._data = data
		self.shape = shape
		self._getter_getter = ndgetter[len(self.shape)]
		self._setter_getter = ndgetter[len(self.shape) - 1]
		self._iterator = nditerator[len(self.shape)]
		self._initializer = ndinitializor[len(self.shape)]
	
	def __len__(self):
		return len(self._data)
	
	def __getitem__(self, key):
		return self._data[key]
	
	def __setitem__(self, key, value):
		self._data[key] = value
	
	def __iter__(self):
		cls = type(self)
		sub_shape = self.shape[1:]
		if 0 < len(sub_shape):
			return (cls(v, sub_shape) for v in self._data)
		else:
			return iter(self._data)
	
	def at(self, indices : Tuple[int]):
		return self._getter_getter(self._data, indices)
	
	def set_at(self, indices : Tuple[int], value):
		self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def fill(self, value):
		for indices in self._iterator(self.shape):
			self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def apply_indices_inplace(self, func : Callable[[Any, Tuple[int]], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]], indices)
	def apply_inplace(self, func : Callable[[Any], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]])
	def apply_indices(self, func : Callable[[Any, Tuple[int]], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices), indices)
		return nd(new_data, self.shape)
	def apply(self, func : Callable[[Any], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices))
		return nd(new_data, self.shape)
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(element, other))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(other, element))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(element, other))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(other, element))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(ndinitializor[0](shape, fill_value), (shape,))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		return cls(ndinitializor[len(shape)](shape, fill_value), shape)
	
	@classmethod
	def list_fromiter(cls, iterable : Iterable, ndim=1):
		if ndim == 1:
			return list(iterable)
		else:
			return list(map(cls.list_fromiter, iterable))
	
	@classmethod
	def fromiter(cls, iterable : Iterable, ndim=1):
		new_data = cls.list_fromiter(iterable, ndim)
		return cls(new_data, ndshapegetter[ndim](new_data))
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			partial_func = partial(func, *left_args, **left_kwargs, **right_kwargs)
			return cls.fromiter(
				partial_func(v, *right_args)
				for v in a._data
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			partial_func = partial(func, *left_args, **left_kwargs, **right_kwargs)
			return a.apply(lambda v: partial_func(v, *right_args))
		return nd_func
	
	@classmethod
	def sorted(cls, iterable : Iterable):
		new_data = sorted(iterable)
		return cls(new_data, (len(new_data),))


def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = nd.sorted(set(C))
ys = nd.sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Any, Callable, Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a, i: a,
	lambda a, i: a[i[0]],
	lambda a, i: a[i[0]][i[1]],
	lambda a, i: a[i[0]][i[1]][i[2]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]],
)
ndinitializor = (
	lambda s, v: [v] * s,
	lambda s, v: [v] * s[0],
	lambda s, v: [[v] * s[1] for _ in range(s[0])],
	lambda s, v: [[[v] * s[2] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[v] * s[3] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[v] * s[4] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[[v] * s[5] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
)
ndshapegetter = (
	lambda a: len(a),
	lambda a: (len(a),),
	lambda a: (len(a), len(a[0])),
	lambda a: (len(a), len(a[0]), len(a[0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0]), len(a[0][0][0][0][0])),
)
nditerator = (
	lambda s: s,
	lambda s: ((i,) for i in range(s[0])),
	lambda s: ((i, j) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m, n) for n in range(s[5]) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
)

class nd (object):
	__slots__ = [
		'_data',
		'shape',
		'_getter_getter',
		'_setter_getter',
		'_iterator',
		'_initializer'
	]
	
	def __init__(self, data : list, shape : Tuple[int]):
		self._data = data
		self.shape = shape
		self._getter_getter = ndgetter[len(self.shape)]
		self._setter_getter = ndgetter[len(self.shape) - 1]
		self._iterator = nditerator[len(self.shape)]
		self._initializer = ndinitializor[len(self.shape)]
	
	def __len__(self):
		return len(self._data)
	
	def __getitem__(self, key):
		return self._data[key]
	
	def __setitem__(self, key, value):
		self._data[key] = value
	
	def __iter__(self):
		cls = type(self)
		sub_shape = self.shape[1:]
		if 0 < len(sub_shape):
			return (cls(v, sub_shape) for v in self._data)
		else:
			return iter(self._data)
	
	def at(self, indices : Tuple[int]):
		return self._getter_getter(self._data, indices)
	
	def set_at(self, indices : Tuple[int], value):
		self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def fill(self, value):
		for indices in self._iterator(self.shape):
			self._setter_getter(self._data, indices[:-1])[indices[-1]] = value
	
	def apply_indices_inplace(self, func : Callable[[Any, Tuple[int]], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]], indices)
	def apply_inplace(self, func : Callable[[Any], Any]):
		for indices in self._iterator(self.shape):
			a = self._setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]])
	def apply_indices(self, func : Callable[[Any, Tuple[int]], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices), indices)
		return nd(new_data, self.shape)
	def apply(self, func : Callable[[Any], Any]):
		new_data = self._initializer(self.shape, None)
		for indices in self._iterator(self.shape):
			a = self._setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(self.at(indices))
		return nd(new_data, self.shape)
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(element, other))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(other, element))
		else:
			return self.apply_indices(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(element, other))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					element,
					self._getter_getter(other, indices)
				)
			)
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(other, element))
		else:
			self.apply_indices_inplace(
				lambda element, indices: operator(
					self._getter_getter(other, indices),
					element
				)
			)
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(ndinitializor[0](shape, fill_value), (shape,))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		return cls(ndinitializor[len(shape)](shape, fill_value), shape)
	
	@classmethod
	def list_fromiter(cls, iterable : Iterable, ndim=1):
		if ndim == 1:
			return list(iterable)
		else:
			return list(map(cls.list_fromiter, iterable))
	
	@classmethod
	def fromiter(cls, iterable : Iterable, ndim=1):
		new_data = cls.list_fromiter(iterable, ndim)
		return cls(new_data, ndshapegetter[ndim](new_data))
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			partial_func = partial(func, *left_args, **left_kwargs, **right_kwargs)
			return cls.fromiter(
				partial_func(v, *right_args)
				for v in a._data
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			partial_func = partial(func, *left_args, **left_kwargs, **right_kwargs)
			return a.apply(lambda v: partial_func(v, *right_args))
		return nd_func
	
	@classmethod
	def sorted(cls, iterable : Iterable):
		new_data = sorted(iterable)
		return cls(new_data, (len(new_data),))


def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = nd.sorted(set(C))
ys = nd.sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.vectorize(bisect_right, xs)(C)
A = nd.vectorize(bisect_left, ys)(A)
A += 1
B = nd.vectorize(bisect_right, ys)(B)

D = nd.vectorize(bisect_right, ys)(D)
E = nd.vectorize(bisect_left, xs)(E)
E += 1
F = nd.vectorize(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Any, Callable, Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a, i: a,
	lambda a, i: a[i[0]],
	lambda a, i: a[i[0]][i[1]],
	lambda a, i: a[i[0]][i[1]][i[2]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]],
	lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]],
)
ndinitializor = (
	lambda s, v: [v] * s,
	lambda s, v: [v] * s[0],
	lambda s, v: [[v] * s[1] for _ in range(s[0])],
	lambda s, v: [[[v] * s[2] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[v] * s[3] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[v] * s[4] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
	lambda s, v: [[[[[[v] * s[5] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
)
ndshapegetter = (
	lambda a: len(a),
	lambda a: (len(a),),
	lambda a: (len(a), len(a[0])),
	lambda a: (len(a), len(a[0]), len(a[0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0])),
	lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0]), len(a[0][0][0][0][0])),
)
nditerator = (
	lambda s: s,
	lambda s: ((i,) for i in range(s[0])),
	lambda s: ((i, j) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
	lambda s: ((i, j, k, l, m, n) for n in range(s[5]) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
)

class nd (object):
	__slots__ = ['_data', 'shape']
	
	def __init__(self, data : list, shape : Tuple[int]):
		self._data = data
		self.shape = shape
	
	def __len__(self):
		return len(self._data)
	
	def __getitem__(self, key):
		return self._data[key]
	
	def __setitem__(self, key, value):
		self._data[key] = value
	
	def __iter__(self):
		cls = type(self)
		sub_shape = self.shape[1:]
		if 0 < len(sub_shape):
			return (cls(v, sub_shape) for v in self._data)
		else:
			return iter(self._data)
	
	def at(self, indices : Tuple[int]):
		return ndgetter[len(indices)](self._data, indices)
	
	def set_at(self, indices : Tuple[int], value):
		ndgetter[len(indices) - 1](self._data, indices[:-1])[indices[-1]] = value
	
	def fill(self, value):
		for indices in nditerator[len(self.shape)](self.shape):
			ndgetter[len(indices) - 1](self._data, indices[:-1])[indices[-1]] = value
	
	def apply_indices_inplace(self, func : Callable[[Any, Tuple[int]], Any]):
		setter_getter = ndgetter[len(self.shape) - 1]
		for indices in nditerator[len(self.shape)](self.shape):
			a = setter_getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]], indices)
	def apply_inplace(self, func : Callable[[Any], Any]):
		getter = ndgetter[len(self.shape) - 1]
		for indices in nditerator[len(self.shape)](self.shape):
			a = getter(self._data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]])
	def apply_indices(self, func : Callable[[Any, Tuple[int]], Any]):
		setter_getter = ndgetter[len(self.shape) - 1]
		new_data = ndinitializor[len(self.shape)](self.shape, None)
		for indices in nditerator[len(self.shape)](self.shape):
			a = setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]], indices)
		return nd(new_data, self.shape)
	def apply(self, func : Callable[[Any], Any]):
		setter_getter = ndgetter[len(self.shape) - 1]
		new_data = ndinitializor[len(self.shape)](self.shape, None)
		for indices in nditerator[len(self.shape)](self.shape):
			a = setter_getter(new_data, indices[:-1])
			a[indices[-1]] = func(a[indices[-1]])
		return nd(new_data, self.shape)
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(element, other))
		else:
			getter_getter = ndgetter[len(self.shape)]
			return self.apply_indices(
				lambda element, indices: operator(
					element,
					getter_getter(other, indices)
				)
			)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			return self.apply(lambda element: operator(other, element))
		else:
			getter_getter = ndgetter[len(self.shape)]
			return self.apply_indices(
				lambda element, indices: operator(
					getter_getter(other, indices),
					element
				)
			)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			self.apply_inplace(lambda element: operator(element, other))
		else:
			getter_getter = ndgetter[len(self.shape)]
			self.apply_indices_inplace(
				lambda element, indices: operator(
					element,
					getter_getter(other, indices)
				)
			)
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			return self.apply_inplace(lambda element: operator(other, element))
		else:
			getter_getter = ndgetter[len(self.shape)]
			return self.apply_indices_inplace(
				lambda element, indices: operator(
					getter_getter(other, indices),
					element
				)
			)
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(ndinitializor[0](shape, fill_value), (shape,))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		return cls(ndinitializor[len(shape)](shape, fill_value), shape)
	
	@classmethod
	def list_fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return list(iterable)
		else:
			return list(map(cls.list_fromiter, iterable))
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		new_data = cls.list_fromiter(iterable, ndim)
		return cls(new_data, ndshapegetter[ndim](new_data))
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a._data
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a: a,
	lambda a, i: a[i],
	lambda a, i, j: a[i][j],
	lambda a, i, j, k: a[i][j][k],
	lambda a, i, j, k, l: a[i][j][k][l],
	lambda a, i, j, k, l, m: a[i][j][k][l][m],
	lambda a, i, j, k, l, m, n: a[i][j][k][l][m][n],
	lambda a, i, j, k, l, m, n, o: a[i][j][k][l][m][n][o],
	lambda a, i, j, k, l, m, n, o, p: a[i][j][k][l][m][n][o][p],
	lambda a, i, j, k, l, m, n, o, p, q: a[i][j][k][l][m][n][o][p][q],
	lambda a, i, j, k, l, m, n, o, p, q, r: a[i][j][k][l][m][n][o][p][q][r],
)

class nd (list):
	def at(self, indices : Tuple[int]):
		return ndgetter[len(indices)](self, *indices)
	
	def set_at(self, indices : Tuple[int], value):
		ndgetter[len(indices) - 1](self, *indices[:-1])[indices[-1]] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]

ndgetter = (
	lambda a: a,
	lambda a, i: a[i],
	lambda a, i, j: a[i][j],
	lambda a, i, j, k: a[i][j][k],
	lambda a, i, j, k, l: a[i][j][k][l],
	lambda a, i, j, k, l, m: a[i][j][k][l][m],
	lambda a, i, j, k, l, m, n: a[i][j][k][l][m][n],
	lambda a, i, j, k, l, m, n, o: a[i][j][k][l][m][n][o],
	lambda a, i, j, k, l, m, n, o, p: a[i][j][k][l][m][n][o][p],
	lambda a, i, j, k, l, m, n, o, p, q: a[i][j][k][l][m][n][o][p][q],
	lambda a, i, j, k, l, m, n, o, p, q, r: a[i][j][k][l][m][n][o][p][q][r],
)

class nd (list):
	def at(self, indices : Tuple[int]):
		return ndgetter[len(indices)](self, *indices)
	
	def set_at(self, indices : Tuple[int], value):
		*rest_indices, last_index = indices
		ndgetter[len(rest_indices)](self, *rest_indices)[last_index] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]


class nd (list):
	def at(self, position : Tuple[int]):
		return reduce(operator.getitem, position, self)
	
	def set_at(self, position : Tuple[int], value):
		reduce(operator.getitem, position[:-1], self)[position[-1]] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]


class nd (list):
	def at(self, position : Tuple[int]):
		return reduce(operator.getitem, position, self)
	
	def set_at(self, position : Tuple[int], value):
		*indices, last_index = position
		reduce(operator.getitem, indices, self)[last_index] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)

from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.pushed = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		if not self.pushed.at(position):
			self.pushed.set_at(position, True)
			self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		return self._deque[0] if self._deque else None
	
	def pop(self) -> Optional[Tuple[int]]:
		return self._deque.popleft() if self._deque else None
	
	def __bool__(self):
		return bool(self._deque)

from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat
from functools import partial, reduce
from typing import Iterable, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]


class nd (list):
	def at(self, position : Tuple[int]):
		return reduce(operator.getitem, position, self)
	
	def set_at(self, position : Tuple[int], value):
		*indices, last_index = position
		reduce(operator.getitem, indices, self)[last_index] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func



def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)
from collections import deque
import operator
from typing import Optional


class BreadthFirstSearch (object):
	def __init__(self, shape : Tuple[int]):
		self.shape = shape
		self.visited = nd.zeros(shape, type=bool)
		self._deque = deque()
	
	def push(self, position : Tuple[int]):
		self._deque.append(position)
	
	def peek(self) -> Optional[Tuple[int]]:
		while self._deque:
			position = self._deque[0]
			if self.visited.at(position):
				self._deque.popleft()
				continue
			return position
		return None
	
	def pop(self) -> Optional[Tuple[int]]:
		if (position := self.peek()) is None:
			return None
		self.visited.set_at(position, True)
		return position
	
	def __bool__(self):
		return self.peek() is not None
from bisect import bisect_left, bisect_right


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c][y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)

bfs = BreadthFirstSearch(shape=(len(xs)+1, len(ys)+1))
bfs.push(cow)

area = 0
while bfs:
	xi, yi = bfs.pop()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi][yi]:
			bfs.push((xi-1, yi))
		if not x_guard[xi+1][yi]:
			bfs.push((xi+1, yi))
		if not y_guard[xi][yi]:
			bfs.push((xi, yi-1))
		if not y_guard[xi][yi+1]:
			bfs.push((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import operator
from itertools import repeat, islice
from functools import partial, reduce
from typing import Iterable, MutableSequence, Optional, Tuple, Union

Indices = Union[int, slice, Tuple[Union[int, slice]]]


class nd (list):
	def __getitem__(self, indices : Union[int, slice, Tuple[Union[int, slice]]]):
		if isinstance(indices, (int, slice)):
			return super().__getitem__(indices)
		else:
			return reduce(operator.getitem, indices, self)
	
	def __setitem__(self, indices : Union[int, slice, Tuple[Union[int, slice]]], value):
		if isinstance(indices, (int, slice)):
			super().__setitem__(indices, value)
		else:
			*rest_indices, last_index = indices
			self[rest_indices][last_index] = value
	
	def fill(self, value):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(value)
			else:
				super().__setitem__(i, value)
	
	def apply(self, func):
		return type(self)(
			(
				element.apply(func)
				if isinstance(element, nd)
				else func(element)
			)
			for element in self
		)
	
	def apply_inplace(self, func):
		for i in range(len(self)):
			element = super().__getitem__(i)
			if isinstance(element, nd):
				element.apply_inplace(func)
			else:
				super().__setitem__(i, func(element))
	
	def operate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.operate(other_element, operator)
				if isinstance(element, nd)
				else operator(element, other_element)
			)
			for element, other_element in zip(self, other)
		)
	def roperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		return type(self)(
			(
				element.roperate(other_element, operator)
				if isinstance(element, nd)
				else operator(other_element, element)
			)
			for element, other_element in zip(self, other)
		)
	def ioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.ioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(element, other_element))
		return self
	def rioperate(self, other, operator):
		if not isinstance(other, list):
			other = repeat(other)
		for i, (element, other_element) in enumerate(zip(self, other)):
			if isinstance(element, nd):
				element.rioperate(other_element, operator)
			else:
				super().__setitem__(i, operator(other_element, element))
		return self
	def __add__(self, other):
		return self.operate(other, operator.add)
	def __radd__(self, other):
		return self.roperate(other, operator.add)
	def __iadd__(self, other):
		return self.ioperate(other, operator.add)
	def __sub__(self, other):
		return self.operate(other, operator.sub)
	def __rsub__(self, other):
		return self.roperate(other, operator.sub)
	def __isub__(self, other):
		return self.ioperate(other, operator.sub)
	def __mul__(self, other):
		return self.operate(other, operator.mul)
	def __rmul__(self, other):
		return self.roperate(other, operator.mul)
	def __imul__(self, other):
		return self.ioperate(other, operator.mul)
	def __matmul__(self, other):
		return self.operate(other, operator.matmul)
	def __rmatmul__(self, other):
		return self.roperate(other, operator.matmul)
	def __imatmul__(self, other):
		return self.ioperate(other, operator.matmul)
	def __truediv__(self, other):
		return self.operate(other, operator.truediv)
	def __rtruediv__(self, other):
		return self.roperate(other, operator.truediv)
	def __itruediv__(self, other):
		return self.ioperate(other, operator.truediv)
	def __floordiv__(self, other):
		return self.operate(other, operator.floordiv)
	def __rfloordiv__(self, other):
		return self.roperate(other, operator.floordiv)
	def __ifloordiv__(self, other):
		return self.ioperate(other, operator.floordiv)
	def __mod__(self, other):
		return self.operate(other, operator.mod)
	def __rmod__(self, other):
		return self.roperate(other, operator.mod)
	def __imod__(self, other):
		return self.ioperate(other, operator.mod)
	def __divmod__(self, other):
		return self.operate(other, operator.divmod)
	def __rdivmod__(self, other):
		return self.roperate(other, operator.divmod)
	def __idivmod__(self, other):
		return self.ioperate(other, operator.divmod)
	def __pow__(self, other):
		return self.operate(other, operator.pow)
	def __rpow__(self, other):
		return self.roperate(other, operator.pow)
	def __ipow__(self, other):
		return self.ioperate(other, operator.pow)
	def __lshift__(self, other):
		return self.operate(other, operator.lshift)
	def __rlshift__(self, other):
		return self.roperate(other, operator.lshift)
	def __ilshift__(self, other):
		return self.ioperate(other, operator.lshift)
	def __rshift__(self, other):
		return self.operate(other, operator.rshift)
	def __rrshift__(self, other):
		return self.roperate(other, operator.rshift)
	def __irshift__(self, other):
		return self.ioperate(other, operator.rshift)
	def __and__(self, other):
		return self.operate(other, operator.and_)
	def __rand__(self, other):
		return self.roperate(other, operator.and_)
	def __iand__(self, other):
		return self.ioperate(other, operator.and_)
	def __xor__(self, other):
		return self.operate(other, operator.xor)
	def __rxor__(self, other):
		return self.roperate(other, operator.xor)
	def __ixor__(self, other):
		return self.ioperate(other, operator.xor)
	def __or__(self, other):
		return self.operate(other, operator.or_)
	def __ror__(self, other):
		return self.roperate(other, operator.or_)
	def __ior__(self, other):
		return self.ioperate(other, operator.or_)
	def __neg__(self, other):
		return self.apply(other, operator.neg)
	def __pos__(self, other):
		return self.apply(other, operator.pos)
	def __abs__(self, other):
		return self.apply(other, operator.abs)
	def __invert__(self, other):
		return self.apply(other, operator.invert)
	def __lt__(self, other):
		return self.operate(other, operator.lt)
	def __le__(self, other):
		return self.operate(other, operator.le)
	def __eq__(self, other):
		return self.operate(other, operator.eq)
	def __ne__(self, other):
		return self.operate(other, operator.ne)
	def __gt__(self, other):
		return self.operate(other, operator.gt)
	def __ge__(self, other):
		return self.operate(other, operator.ge)
	
	@classmethod
	def full(cls, fill_value, shape : Union[int, Tuple[int], Iterable[int]]):
		if isinstance(shape, int):
			return cls(repeat(fill_value, shape))
		elif not isinstance(shape, tuple):
			shape = tuple(shape)
		
		if len(shape) == 1:
			return cls(repeat(fill_value, shape[0]))
		else:
			return cls(
				map(
					partial(cls.full, fill_value),
					repeat(shape[1:], shape[0])
				)
			)
	
	@classmethod
	def fromiter(cls, iterable, ndim=1):
		if ndim == 1:
			return cls(iterable)
		else:
			return cls(
				cls.fromiter(element, ndim-1)
				for element in iterable
			)
	
	@classmethod
	def nones(cls, shape : Union[int, Tuple[int], Iterable[int]]):
		return cls.full(None, shape)
	
	@classmethod
	def zeros(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(0), shape)
	
	@classmethod
	def ones(cls, shape : Union[int, Tuple[int], Iterable[int]], type=int):
		return cls.full(type(1), shape)
	
	@classmethod
	def partial_map(cls, func, *left_args, **left_kwargs):
		def nd_func(a, *right_args, **right_kwargs):
			return cls.fromiter(
				func(*left_args, v, *right_args, **left_kwargs, **right_kwargs)
				for v in a
			)
		return nd_func
	
	@classmethod
	def vectorize(cls, func, *left_args, **left_kwargs):
		def nd_func(a : nd, *right_args, **right_kwargs):
			return a.apply(lambda v: func(
				*left_args, v, *right_args, **left_kwargs, **right_kwargs
			))
		return nd_func


def bytes_to_str(x : bytes):
	return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
	return nd(inputs(func, **kwargs))

def inputs_2d(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		(inputs(func, **kwargs) for _ in range(nrows)),
		ndim=2
	)

def inputs_2d_T(nrows : int, func=bytes_to_str, **kwargs):
	return nd.fromiter(
		zip(*(inputs(func, **kwargs) for _ in range(nrows))),
		ndim=2
	)
from bisect import bisect_left, bisect_right
from collections import deque


N, M = inputs(int)
A, B, C = inputs_2d_T(N, int)
D, E, F = inputs_2d_T(M, int)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)
y_guard = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

C = nd.partial_map(bisect_right, xs)(C)
A = nd.partial_map(bisect_left, ys)(A)
A += 1
B = nd.partial_map(bisect_right, ys)(B)

D = nd.partial_map(bisect_right, ys)(D)
E = nd.partial_map(bisect_left, xs)(E)
E += 1
F = nd.partial_map(bisect_right, xs)(F)

for a, b, c in zip(A, B, C):
	for y in range(a, b):
		x_guard[c, y] = True

for d, e, f in zip(D, E, F):
	for x in range(e, f):
		y_guard[x, d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = nd.zeros((len(xs)+1, len(ys)+1), type=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if visited[xi, yi]:
		continue
	
	visited[xi, yi] = True
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not x_guard[xi, yi]:
			nexts.append((xi-1, yi))
		if not x_guard[xi+1, yi]:
			nexts.append((xi+1, yi))
		if not y_guard[xi, yi]:
			nexts.append((xi, yi-1))
		if not y_guard[xi, yi+1]:
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
from bisect import bisect_left, bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = np.unique(C)
ys = np.unique(D)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	x_guard[c, a:b] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	y_guard[e:f, d] = True

cow = (
	np.searchsorted(xs, 0, side='right'),
	np.searchsorted(ys, 0, side='right')
)
nexts = deque([cow])
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
visited[cow] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1, yi] and not x_guard[xi, yi]:
			visited[xi-1, yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1, yi] and not x_guard[xi+1, yi]:
			visited[xi+1, yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi, yi-1] and not y_guard[xi, yi]:
			visited[xi, yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi, yi+1] and not y_guard[xi, yi+1]:
			visited[xi, yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = np.unique(C)
ys = np.unique(D)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

C = np.searchsorted(xs, C, side='right')
A = np.searchsorted(ys, A, side='left') + 1
B = np.searchsorted(ys, B, side='right')

D = np.searchsorted(ys, D, side='right')
E = np.searchsorted(xs, E, side='left') + 1
F = np.searchsorted(xs, F, side='right')

for a, b, c in zip(A, B, C):
	x_guard[c, a:b] = True

for d, e, f in zip(D, E, F):
	y_guard[e:f, d] = True

cow = (
	np.searchsorted(xs, 0, side='right'),
	np.searchsorted(ys, 0, side='right')
)
nexts = deque([cow])
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
visited[cow] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1, yi] and not x_guard[xi, yi]:
			visited[xi-1, yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1, yi] and not x_guard[xi+1, yi]:
			visited[xi+1, yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi, yi-1] and not y_guard[xi, yi]:
			visited[xi, yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi, yi+1] and not y_guard[xi, yi+1]:
			visited[xi, yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = np.unique(np.concatenate((C, E, F)))
ys = np.unique(np.concatenate((A, B, D)))

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

A = np.searchsorted(ys, A, side='right')
B = np.searchsorted(ys, B, side='right')
C = np.searchsorted(xs, C, side='right')
D = np.searchsorted(ys, D, side='right')
E = np.searchsorted(xs, E, side='right')
F = np.searchsorted(xs, F, side='right')

for a, b, c in zip(A, B, C):
	x_guard[c, a:b] = True

for d, e, f in zip(D, E, F):
	y_guard[e:f, d] = True

nexts = deque()
x0, y0 = (
	np.searchsorted(xs, 0, side='right'),
	np.searchsorted(ys, 0, side='right')
)
nexts.append((x0, y0))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
visited[x0, y0] = True
area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1, yi] and not x_guard[xi, yi]:
			visited[xi-1, yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1, yi] and not x_guard[xi+1, yi]:
			visited[xi+1, yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi, yi-1] and not y_guard[xi, yi]:
			visited[xi, yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi, yi+1] and not y_guard[xi, yi+1]:
			visited[xi, yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = np.unique(np.concatenate((C, E, F)))
ys = np.unique(np.concatenate((A, B, D)))

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

A = np.searchsorted(ys, A, side='right')
B = np.searchsorted(ys, B, side='right')
C = np.searchsorted(xs, C, side='right')
D = np.searchsorted(ys, D, side='right')
E = np.searchsorted(xs, E, side='right')
F = np.searchsorted(xs, F, side='right')

for a, b, c in zip(A, B, C):
	x_guard[c, a:b] = True

for d, e, f in zip(D, E, F):
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((
	np.searchsorted(xs, 0, side='right'),
	np.searchsorted(ys, 0, side='right')
))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = np.fromiter({*C, *E, *F}, dtype=int)
ys = np.fromiter({*A, *B, *D}, dtype=int)
xs.sort()
ys.sort()
x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

A = np.searchsorted(ys, A, side='right')
B = np.searchsorted(ys, B, side='right')
C = np.searchsorted(xs, C, side='right')
D = np.searchsorted(ys, D, side='right')
E = np.searchsorted(xs, E, side='right')
F = np.searchsorted(xs, F, side='right')

for a, b, c in zip(A, B, C):
	x_guard[c, a:b] = True

for d, e, f in zip(D, E, F):
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((
	np.searchsorted(xs, 0, side='right'),
	np.searchsorted(ys, 0, side='right')
))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T

xs = sorted({*C, *E, *F})
ys = sorted({*A, *B, *D})
x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	return data.reshape(nrows, ncols)

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
from itertools import chain


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.fromiter(
		chain.from_iterable(
			inputs(type) for _ in range(nrows)
		),
		dtype=type
	)
	for i in range(nrows):
		data[i, :] = input_row(ncols, type, **kwargs)
	return data

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.zeros((nrows, ncols), dtype=type)
	for i in range(nrows):
		data[i, :] = input_row(ncols, type, **kwargs)
	return data

from collections import deque
from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = deque()
nexts.append((bisect_right(xs, 0), bisect_right(ys, 0)))
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.popleft()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.zeros((nrows, ncols), dtype=type)
	for i in range(nrows):
		data[i, :] = input_row(ncols, type, **kwargs)
	return data

from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)
y_guard = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	x_guard[c, a:b] = True

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	y_guard[e:f, d] = True

nexts = [(bisect_right(xs, 0), bisect_right(ys, 0))]
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.pop()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if not x_guard[xi, yi]:
				nexts.append((xi-1, yi))
			if not x_guard[xi+1, yi]:
				nexts.append((xi+1, yi))
			if not y_guard[xi, yi]:
				nexts.append((xi, yi-1))
			if not y_guard[xi, yi+1]:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.readline().split(sep=sep, maxsplit=maxsplit))

def input_row(n : int, type=np.int, **kwargs):
	return np.fromiter(inputs(type, **kwargs), dtype=type)

def input_2d(nrows : int, ncols : int, type=np.int, **kwargs):
	data = np.zeros((nrows, ncols), dtype=type)
	for i in range(nrows):
		data[i, :] = input_row(ncols, type, **kwargs)
	return data

from bisect import bisect_right
import numpy as np


N, M = inputs(int)
A, B, C = input_2d(N, 3).T
D, E, F = input_2d(M, 3).T
xs = set()
ys = set()

for i in range(N):
	xs.add(C[i])
	ys.add(A[i])
	ys.add(B[i])

for i in range(M):
	ys.add(D[i])
	xs.add(E[i])
	xs.add(F[i])

xs = sorted(xs)
ys = sorted(ys)

x_closed = set()
y_closed = set()
for i in range(N):
	c = bisect_right(xs, C[i])
	a = bisect_right(ys, A[i])
	b = bisect_right(ys, B[i])
	for y in range(a, b):
		x_closed.add(((c-1, c), y))

for i in range(M):
	d = bisect_right(ys, D[i])
	e = bisect_right(xs, E[i])
	f = bisect_right(xs, F[i])
	for x in range(e, f):
		y_closed.add((x, (d-1, d)))

nexts = [(bisect_right(xs, 0), bisect_right(ys, 0))]
visited = np.zeros((len(xs)+1, len(ys)+1), dtype=bool)

area = 0

while nexts:
	xi, yi = nexts.pop()
	if not visited[xi, yi]:
		visited[xi, yi] = True
		if 0 < xi < len(xs) and 0 < yi < len(ys):
			area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
			if ((xi-1, xi), yi) not in x_closed:
				nexts.append((xi-1, yi))
			if ((xi, xi+1), yi) not in x_closed:
				nexts.append((xi+1, yi))
			if (xi, (yi-1, yi)) not in y_closed:
				nexts.append((xi, yi-1))
			if (xi, (yi, yi+1)) not in y_closed:
				nexts.append((xi, yi+1))
		else:
			area = 'INF'
			break

print(area)



Fixed submission:
import sys
from bisect import bisect_left, bisect_right


def inputs(func=lambda x: x, sep=None, maxsplit=-1):
	return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def input_2d_T(nrows : int, ncols : int, type=int, **kwargs):
	data = [[None] * nrows for _ in range(ncols)]
	for i in range(nrows):
		for j, v in enumerate(inputs(type)):
			data[j][i] = v
	return data

from collections import deque

N, M = inputs(int)
A, B, C = input_2d_T(N, 3)
D, E, F = input_2d_T(M, 3)
xs = sorted(set(C))
ys = sorted(set(D))

x_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
y_guard = [[False for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]

for a, b, c in zip(A, B, C):
	c = bisect_right(xs, c)
	a = bisect_left(ys, a) + 1
	b = bisect_right(ys, b)
	for i in range(a, b):
		x_guard[c][i] = True

for d, e, f in zip(D, E, F):
	d = bisect_right(ys, d)
	e = bisect_left(xs, e) + 1
	f = bisect_right(xs, f)
	for i in range(e, f):
		y_guard[i][d] = True

cow = (
	bisect_right(xs, 0),
	bisect_right(ys, 0)
)
nexts = deque([cow])
visited = [[0 for _ in range(len(ys)+1)] for _ in range(len(xs)+1)]
visited[cow[0]][cow[1]] = True

area = 0

while nexts:
	xi, yi = nexts.popleft()
	
	if 0 < xi < len(xs) and 0 < yi < len(ys):
		area += (xs[xi] - xs[xi-1]) * (ys[yi] - ys[yi-1])
		if not visited[xi-1][yi] and not x_guard[xi][yi]:
			visited[xi-1][yi] = True
			nexts.append((xi-1, yi))
		if not visited[xi+1][yi] and not x_guard[xi+1][yi]:
			visited[xi+1][yi] = True
			nexts.append((xi+1, yi))
		if not visited[xi][yi-1] and not y_guard[xi][yi]:
			visited[xi][yi-1] = True
			nexts.append((xi, yi-1))
		if not visited[xi][yi+1] and not y_guard[xi][yi+1]:
			visited[xi][yi+1] = True
			nexts.append((xi, yi+1))
	else:
		area = 'INF'
		break

print(area)