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.

Input

The first line of the input contains integer n (1 ≤ n < 1015).

Output

Print expected minimal number of digits 1.

Examples

Input

121


Output

6
import sys

def getline():
    return raw_input()

def getint():
    return int(raw_input())

def getints():
    return map(int, raw_input().split())

def getlist():
    return raw_input().split()

#sys.stdout = open("in.in", "w")

def dfs(d, n):
    if n in d:
        return d[n]
    one, nextOne = 1, 11
    while nextOne <= n:
        one, nextOne = nextOne, nextOne * 10 + 1
    if one == n:
        return str(one).count('1')
    if nextOne - n < n:
        d[n] = min(dfs(d, n - one) + str(one).count('1'),
                    dfs(d, nextOne - n) + str(nextOne).count('1'))
        return d[n]
    d[n] = dfs(d, n - one) + str(one).count('1')
    return d[n]

n = input()
d = {}
ans = dfs(d, n)
print ans
