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>).
import numpy as np

def ones(number):
    return int((10**number-1)/9)

def digits(number):
    return len(str(number))

array = np.ones((10**6))*-1

def f(number):
    if number < 10**6 and array[number] != -1:
        return array[number]
    if number == 0:
        return 0
    if number == ones(digits(number)):
        return digits(number)
    d = digits(number)
    one = ones(d)
    lower = int(number/one)
    higher = int(number/one) + 1
    if ones(d+1) < 2*number:
        if number < 10**6:
            array[number] = min(f(ones(d+1) - number) + d+1, f(number-lower*one) + lower*d, f(higher*one-number) + higher*d)
            return array[number]
        else:
            return min(f(ones(d+1) - number) + d+1, f(number-lower*one) + lower*d, f(higher*one-number) + higher*d)
    if lower == 0:
        if number < 10 ** 6:
            array[number] = f(higher*one-number) + higher*d
            return array[number]
        else:
            return f(higher*one-number) + higher*d

    if number < 10 ** 6:
        array[number] = min(f(number-lower*one) + lower*d, f(higher*one-number) + higher*d)
        return array[number]
    else:
        return min(f(number-lower*one) + lower*d, f(higher*one-number) + higher*d)

a = int(input())
print(int(f(a)))
