Problem p03713 (python)

Fixed submission:
import math

h, w = map(int, input().split())
diff_2 = min(h, w)

if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()

elif h % 2 == 0 and w %2 == 0:
    if h >= w:
        side1 = int(w/2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    else:
        side1 = int(h / 2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    print(min(diff, diff_2))


elif h % 2 == 0 or w % 2 == 0:
    if h % 2 == 0:
        side1 = int(h/2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
                # print(i, diff)
        print(min(diff, diff_2))
    else:
        side1 = int(w / 2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
        print(min(diff, diff_2))
else:
    if h >= w:
        side1 = math.ceil(h/2)
        side2 = math.floor(h/2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (w-i) * h
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))
    else:
        side1 = math.ceil(w/2)
        side2 = math.floor(w/2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (h-i) * w
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))

Buggy submission (verdict: Runtime Error):
import math

h, w = map(int, input().split())
diff_2 = min(h, w)

if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()

elif h % 2 == 0 and w %2 == 0:
    if h >= w:
        side1 = int(w/2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    else:
        side1 = int(h / 2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    print(min(diff, diff_2))


elif h % 2 == 0 or w % 2 == 0:
    if h % 2 == 0:
        side1 = int(h/2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
                # print(i, diff)
        print(min(diff, diff_2))
    else:
        side1 = int(w / 2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
        print(min(diff, diff_2))
else:
    if h >= w:
        side1 = math.ceil(h/2)
        side2 = math.floor(h/2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (w-i) * h
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))
    else:
        side1 = math.ceil(w/2)
        side2 = math.floor(w/2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (h-i) * w
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))