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>).
one = []
for i in range(17):
    one.append(0)


def code():
    i = 1
    j = 0
    k = 0
    one[0] = 0
    for i in range(1, 17):
        one[i] = one[i-1]*10+1
    n = int(input())
    print(df(n, 16))


def df(n, i):
    k = int(n/one[i])
    n %= one[i]
    if n == 0:
        return k*i
    else:
        return k*i+min(i+df(one[i]-n, i-1), df(n, i-1))


code()
