Prof. Vasechkin wants to represent positive integer *n* as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.
The first line of the input contains integer *n* (1 ≤ *n* &lt; 10<sup class="upper-index">15</sup>).
d = {}
start = 1
for i in range(1,17):
    d[i] = start
    start = start * 10 + 1
d[0] = 1
num = input()
def helper(curr,d):
    if curr == "0":
        return 0
    length = len(curr)
    value = d[length]
    if int(curr) < value:
        ub = value
        lb = d[length - 1]
    else:
        ub = d[length + 1]
        lb = value
    return min(int(curr) // lb * len(str(lb)) + helper(str(int(curr) % lb),d),(ub // int(curr)) * len(str(ub)) + helper(str(ub % int(curr)),d))
print(helper(num,d))