import string, re
DIGITS = string.digits + string.ascii_uppercase

def check_base(base):
	if not isinstance(base, int) or not 2 <= base <= 36: raise ValueError(f"Неверное основание: {base!r}.")

def to_base(n, base):
	check_base(base)
	result = ""
	while True:
		result = DIGITS[n % base] + result
		n //= base
		if n == 0: break
	return result

def from_base(sn, base):
	check_base(base)
	result, multiplier = 0, 1
	for sdig in reversed(sn):
		if '0' <= sdig <= '9': digit = ord(sdig) - ord('0')
		elif 'A' <= sdig <= 'Z': digit = ord(sdig) - ord('A') + 10
		elif 'a' <= sdig <= 'z': digit = ord(sdig) - ord('a') + 10
		else: digit = None
		if digit is None or digit >= base: raise ValueError(f"Неверный {base}-разряд: {sdig!r}.")
		result += multiplier * digit
		multiplier *= base
	return result

input_re = re.compile("""
	\s* (?P<inp_value> [0-9A-Z]+)
	(
		\s* \( (?P<inp_base> \d+) \)
	)?
	(
		\s* -> \s* (?P<target_base> \d+)
	)?
	\s*""", re.VERBOSE)

match = input_re.fullmatch(input("Введите запрос, например, FF(16)->2: "))
if match:
	inp_value, inp_base, target_base = match['inp_value'], match['inp_base'], match['target_base']
	inp_basev, target_basev = inp_base and int(inp_base), target_base and int(target_base)
	if inp_base is None: inp_basev = 16 if target_basev == 10 else 10
	if target_base is None: target_basev = 16 if inp_basev == 10 else 10

	result = to_base(from_base(inp_value, inp_basev), target_basev)
	print(f"\n{inp_value}({inp_basev}) = {result}({target_basev})")
else:
	print("Не понял.")