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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    s = set()
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            s.add(i * h + j)
    vl = s

    s = set()
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            s.add(i * h + j)
    hl = s
    # print(vl,hl)

    tt = [[False] * w for _ in range(h)]
    si, sj = cox[0], coy[0]
    stack = [(si, sj)]
    tt[si][sj] = True

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if i * h + j not in vl:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if ni * h + nj not in vl:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if i * h + j not in hl:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if ni * h + nj not in hl:
            if ni == h - 1: inf()
            move(ni, nj)
    # p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()


Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    h=len(dex)
    w=len(dey)

    tt=[[9]*w for _ in range(h)]
    s=set()
    for x0,x1,y in vl:
        j=coy[y]
        for i in range(cox[x0],cox[x1]):
            s.add(i*h+j)
    vl=s
    s=set()
    for y0,y1,x in hl:
        i=cox[x]
        for j in range(coy[y0],coy[y1]):
            s.add(i*h+j)
    hl=s
    #print(vl,hl)

    si,sj=cox[0],coy[0]
    stack=[(si,sj)]
    tt[si][sj]=1

    def inf():
        print("INF")
        exit()

    def move(ni,nj):
        if tt[ni][nj] == 9:
            tt[ni][nj] = 1
            stack.append((ni, nj))

    while stack:
        i,j=stack.pop()

        ni, nj = i, j - 1
        if i * h + j not in vl:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if ni * h + nj not in vl:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i-1, j
        if i * h + j not in hl:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i+1, j
        if ni * h + nj not in hl:
            if ni == h - 1: inf()
            move(ni, nj)

    ans=0
    for i in range(h-1):
        for j in range(w-1):
            if tt[i][j]==1:
                ans+=(dex[i+1]-dex[i])*(dey[j+1]-dey[j])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    h=len(dex)*2
    w=len(dey)*2

    tt=[[9]*w for _ in range(h)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2):
            tt[i][coy[y]*2]=0
    for y0,y1,x in hl:
        for j in range(coy[y0]*2,coy[y1]*2):
            tt[cox[x]*2][j]=0
    #p2D(tt)

    si,sj=cox[0]*2,coy[0]*2
    stack=[(si,sj)]
    tt[si][sj]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=h or nj>=w:continue
            if tt[ni][nj]!=9:continue
            if ni==0 or nj==0 or ni==h-1 or nj==w-1:
                print("INF")
                #p2D(tt)
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))
    #p2D(tt)

    ans=0
    for i in range(1,h,2):
        for j in range(1,w,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dey[j//2])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    xn=len(dex)
    yn=len(dey)

    tt=[[9]*yn*2 for _ in range(xn*2)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2):
            tt[i][coy[y]*2]=0
    for y0,y1,x in hl:
        for j in range(coy[y0]*2,coy[y1]*2):
            tt[cox[x]*2][j]=0
    #p2D(tt)

    si,sj=cox[0]*2,coy[0]*2
    stack=[(si,sj)]
    tt[si][sj]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=2*xn or nj>=2*yn:continue
            if tt[ni][nj]!=9:continue
            if ni==0 or nj==0 or ni==2*xn-1 or nj==2*yn-1:
                print("INF")
                #p2D(tt)
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))
    #p2D(tt)

    ans=0
    for i in range(1,xn*2,2):
        for j in range(1,yn*2,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dex[j//2])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    xn=len(dex)
    yn=len(dey)

    tt=[[9]*yn*2 for _ in range(xn*2)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2):
            tt[i][coy[y]*2]=0
    for y0,y1,x in hl:
        for j in range(coy[y0]*2,coy[y1]*2):
            tt[cox[x]*2][j]=0
    p2D(tt)

    si,sj=cox[0]*2,coy[0]*2
    stack=[(si,sj)]
    tt[si][sj]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=2*xn or nj>=2*yn:continue
            if tt[ni][nj]!=9:continue
            if ni==0 or nj==0 or ni==2*xn-1 or nj==2*yn-1:
                print("INF")
                #p2D(tt)
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))
    p2D(tt)

    ans=0
    for i in range(1,xn*2,2):
        for j in range(1,yn*2,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dex[j//2])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

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

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    xn=len(dex)
    yn=len(dey)

    tt=[[9]*yn*2 for _ in range(xn*2)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2+1):
            tt[i][coy[y]*2]=0
    for y0,y1,x in hl:
        for j in range(coy[y0]*2,coy[y1]*2+1):
            tt[cox[x]*2][j]=0
    #p2D(tt)

    si,sj=cox[0]*2,coy[0]*2
    stack=[(si,sj)]
    tt[si][sj]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=2*xn or nj>=2*yn:continue
            if tt[ni][nj]!=9:continue
            if ni==0 or nj==0 or ni==2*xn-1 or nj==2*yn-1:
                print("INF")
                #p2D(tt)
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))
    #p2D(tt)

    ans=0
    for i in range(1,xn*2,2):
        for j in range(1,yn*2,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dex[j//2])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    xn=len(dex)
    yn=len(dey)

    tt=[[-1]*yn*2 for _ in range(xn*2)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2+1):
            tt[i][coy[y]*2]=0
    for x0,x1,y in hl:
        for i in range(coy[x0]*2,coy[x1]*2+1):
            tt[cox[y]*2][i]=0

    stack=[(cox[0]*2,coy[0]*2)]
    tt[cox[0]*2][coy[0]*2]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=2*xn or nj>=2*yn:continue
            if tt[ni][nj]!=-1:continue
            if ni==0 or nj==0 or ni==2*xn-1 or nj==2*yn-1:
                print("INF")
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))

    ans=0
    for i in range(1,xn*2,2):
        for j in range(1,yn*2,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dex[j//2])
    print(ans)

main()

Fixed submission:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, m = MI()
    xx = set([0])
    yy = set([0])
    vl = []
    hl = []
    for _ in range(n):
        a, b, c = MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a, b, c))
    for _ in range(m):
        a, b, c = MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b, c, a))

    dex = list(sorted(xx))
    dey = list(sorted(yy))
    cox = {x: i for i, x in enumerate(dex)}
    coy = {x: i for i, x in enumerate(dey)}
    h = len(dex)
    w = len(dey)

    vlt=[[1]*w for _ in range(h)]
    for x0, x1, y in vl:
        j = coy[y]
        for i in range(cox[x0], cox[x1]):
            vlt[i][j]=0

    hlt=[[1]*w for _ in range(h)]
    for y0, y1, x in hl:
        i = cox[x]
        for j in range(coy[y0], coy[y1]):
            hlt[i][j]=0
    #p2D(vlt)
    #print()
    #p2D(hlt)

    tt = [[False] * w for _ in range(h)]

    def inf():
        print("INF")
        exit()

    def move(ni, nj):
        if tt[ni][nj]: return
        tt[ni][nj] = True
        stack.append((ni, nj))

    si, sj = cox[0], coy[0]
    if si==h-1 or sj==w-1:inf()
    stack = [(si, sj)]
    tt[si][sj] = True
    while stack:
        i, j = stack.pop()

        ni, nj = i, j - 1
        if vlt[i][j]:
            if nj == -1: inf()
            move(ni, nj)

        ni, nj = i, j + 1
        if vlt[ni][nj]:
            if nj == w - 1: inf()
            move(ni, nj)

        ni, nj = i - 1, j
        if hlt[i][j]:
            if ni == -1: inf()
            move(ni, nj)

        ni, nj = i + 1, j
        if hlt[ni][nj]:
            if ni == h - 1: inf()
            move(ni, nj)
    #p2D(tt)

    ans = 0
    for i in range(h - 1):
        for j in range(w - 1):
            if tt[i][j]: ans += (dex[i + 1] - dex[i]) * (dey[j + 1] - dey[j])
    print(ans)

main()