import re


american_time_regex = re.compile(r"^\s*(\d+)(?:\:(\d{2}))? ([AP])M to (\d+)(?:\:(\d{2}))? ([AP])\s*M$")


def main():
    print(convert(input("Hours: ")))


def convert(s):
    time = american_time_regex.match(s)
    if time is None:
        raise ValueError("Invalid input time")
    if len(time.groups()) == 4:
        start_h, start_ampm, end_h, end_ampm = time.groups()
        start_m, end_m = 0, 0
    else:
        start_h, start_m, start_ampm, end_h, end_m, end_ampm = time.groups()
        start_m, end_m = map(lambda x: int(x) if x else 0, (start_m, end_m))
    start_h, end_h = map(int, (start_h, end_h))
    if start_h > 12 or end_h > 12:
        raise ValueError
    if (start_ampm == 'P' and start_h != 12) or (start_ampm == 'A' and start_h == 12):
        start_h += 12
    if (end_ampm == 'P' and end_h != 12) or (end_ampm == 'A' and end_h == 12):
        end_h += 12
    if start_h > 24 or end_h > 24 or start_m > 59 or end_m > 59:
        raise ValueError
    start_h = start_h % 24
    end_h = end_h % 24
    return f"{start_h:0>2}:{start_m:0>2} to {end_h:0>2}:{end_m:0>2}"


...


if __name__ == "__main__":
    main()
