#
#
#
# 9876 -> 9 + 8 + 7 + 6 = 30 -> 3 + 0 = 3 ==> 3
#
#


def f(x):
    if x is None or not isinstance(x, int):
        raise ValueError()

    if x < 0:
        raise ValueError()

    if x < 10:
        return x

    total = 0
    num = x
    while True:
        a, b = divmod(num, 10)
        total += b

        if a == 0:
            break
        else:
            num = a

    return f(total)

