def is_valid_binary(s):
    # Check if the string contains only '0' and '1' and is at most 10⁴ characters
    if len(s) > 10**4 or not all(c in '01' for c in s):
        return False
    # Check for leading zeros (except if the string is "0" itself)
    if len(s) > 1 and s[0] == '0':
        return False
    return True

def add_binary(a: str, b: str) -> str:
    result = []
    carry = 0

    # Make both strings the same length by padding with zeros
    max_len = max(len(a), len(b))
    a, b = a.zfill(max_len), b.zfill(max_len)

    # Traverse both strings from right to left
    for i in range(max_len - 1, -1, -1):
        bit_sum = carry + int(a[i]) + int(b[i])
        result.append(str(bit_sum % 2))  # Append the binary sum bit
        carry = bit_sum // 2  # Update the carry

    # If there is a carry left, append it
    if carry:
        result.append("1")

    return ''.join(result[::-1])  # Reverse the result to get the final sum

# Taking input from the user
a = input("Enter first binary number: ").strip()
b = input("Enter second binary number: ").strip()

# Validate input
if not is_valid_binary(a) or not is_valid_binary(b):
    print("Error: Input must only contain '0' and '1', be at most 10⁴ characters long, and have no leading zeros except '0' itself.")
else:
    print("Sum:", add_binary(a, b))
