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>).
n = (lambda x: x - x//10)(int(input()))
ds = [int(s) for s in str(n)[::-1]]
ds.append(0)

def m(ns):
    res = False
    for i in range(len(ns)-1):
        if abs(ns[i+1]+1)*(i+2)+abs(ns[i]-10)*(i+1) < abs(ns[i+1])*(i+2)+abs(ns[i])*(i+1):
            ns[0], ns[i+1], ns[i] = ns[0]-1, ns[i+1]+1, ns[i]-10
            res = True
        elif abs(ns[i+1]-1)*(i+2)+abs(ns[i]+10)*(i+1) < abs(ns[i+1])*(i+2)+abs(ns[i])*(i+1):
            ns[0], ns[i+1], ns[i] = ns[0]+1, ns[i+1]-1, ns[i]+10
            res = True
    return res

# print(ds)
while m(ds):
    pass
    # print(ds)

print(sum([abs(ds[i]*(i+1)) for i in range(len(ds))]))
