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 < 10^15).


-----Output-----

Print expected minimal number of digits 1.


-----Examples-----
Input
121

Output
6

Difficulty: interview

Starter code:

Solutions:
"""
Codeforces Testing Round 10 Problem C

Author  : chaotic_iak
Language: Python 3.3.4
"""

def read(mode=2):
    # 0: String
    # 1: List of strings
    # 2: List of integers
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return [int(x) for x in inputs.split()]

def write(s="\n"):
    if isinstance(s, list): s = " ".join(s)
    s = str(s)
    print(s, end="")

################################################### SOLUTION
def g(n):
    return (10**n-1)//9

def solve(n):
    if n <= 6: return n
    if 7 <= n <= 11: return 13-n
    l = 1
    while g(l) < n: l += 1
    l -= 1
    gl = g(l)
    a = n
    res1 = 0
    res1 += (a // gl) * l
    a %= gl
    res1 += solve(a)
    b = g(l+1) - n
    res2 = l+1
    res2 += (b // gl) * l
    b %= gl
    res2 += solve(b)
    return min(res1, res2)

n, = read()
print(solve(n))

n = int(input())
d = {n: 0}
u = 10 ** (len(str(n)) + 2) // 9
for i in range(len(str(n)) + 1, 0, -1):
    d, e = {}, d
    u //= 10
    for v, c in e.items():
        lim = v//u
        for x in range(-1 - lim, 1 - lim):
            t = v + x * u
            d[t] = min(c + i * abs(x), d.get(t, 999))
print(d[0])

n = int(input())
d = {n: 0}
m = len(str(n)) + 1
u = int('1' * (m+1))
for i in range(m, 0, -1):
    d, e, u = {}, d, u // 10
    for v, c in e.items():
        lim = v // u
        for x in range(-1 - lim, 1 - lim):
            t = v + x * u
            d[t] = min(c + i * abs(x), d.get(t, 999))
print(d[0])

ans=1000000000000
mul = []

def dfs(now, num, opt):
	if num == 0:
		nonlocal ans 
		ans = min(ans, opt)
		return
	if now == -1:
		return
	i=-10
	while i * mul[now] <= num:
		i+=1
	dfs(now-1, num-(i-1)*mul[now], opt+abs(i-1)*(now+1))
	dfs(now-1, num-i*mul[now], opt+abs(i)*(now+1))

def main():
	mul.append(int(1))
	num = int(input())
	for i in range(1,18,1):
		mul.append(mul[i-1]*10)
	for i in range(1,18,1):
		mul[i] += mul[i-1]
	i = 1
	while mul[i] <= num:
		i+=1
	n=i
	dfs(n, num, 0)
	print(ans)
		
main()

import math
acc = []
n = int(input())
def OneNum (num, One):
 onetrans = num // acc[One]
 num = num % acc[One] 
 if num == 0:
  return onetrans*One
 else:
  return onetrans*One + min(OneNum(num, One-1), One + OneNum(acc[One] - num, One - 1))
acc.append(0)
for i in range(1,17):
 acc.append (acc[i-1]*10 + 1)
print (OneNum(n,16))

n = int(input())

d = {}

def count(x):
	if x == 0:
		return 0
	elif x in d:
		return d[x]

	x_s = str(x)
	low = int("1"*len(x_s)) if int("1"*len(x_s)) <= x else int("1"*(len(x_s)-1))
	high = int("1"*len(x_s)) if int("1"*len(x_s)) >= x else int("1"*(len(x_s)+1))

	l_val, h_val = -1, -1
	if abs(x - low) < x:
		l_val = len(str(low)) + count(abs(x - low))
	if abs(x - high) < x:
		h_val = len(str(high)) + count(abs(x - high))

	ret = min(l_val, h_val) if min(l_val, h_val) >= 0 else max(l_val, h_val)
	d[x] = ret
	return ret

print(count(n))

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()


multiplos_de_onze = [0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111, 
                     111111111111, 1111111111111, 11111111111111, 111111111111111, 1111111111111111, 
                     11111111111111111]


def procura_onze(n, i):  # i = 16 pois será as posições do vetor decrementadas
    divisao = int(n / multiplos_de_onze[i])
    resto = n % multiplos_de_onze[i]
    if resto == 0:
        return divisao*i
    else:
        return divisao*i + min(i + procura_onze(multiplos_de_onze[i] - resto, i - 1), procura_onze(resto, i - 1))


def divide():
    n = int(input())
    m = procura_onze(n, 16)
    print(m)


divide()

multiplos_de_onze = [0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111,
                     111111111111, 1111111111111, 11111111111111, 111111111111111, 1111111111111111,
                     11111111111111111]
# Entrada maxima, n maior que 1 e n menor que 10 elevado a 15


def procura_onze(n, i):  # i = 16 - pois será a ultima posição do vetor
    divisao = int(n / multiplos_de_onze[i])
    resto = n % multiplos_de_onze[i]
    if resto == 0:
        return divisao*i
    else:
        return divisao*i + min(i + procura_onze(multiplos_de_onze[i] - resto, i - 1), procura_onze(resto, i - 1))


def divide():
    n = int(input())
    m = procura_onze(n, 16)
    print(m)


divide()


n = int(input())

d = {n: 0}

u = 10 ** (len(str(n)) + 2) // 9

for i in range(len(str(n)) + 1, 0, -1):

    d, e = {}, d

    u //= 10

    for v, c in list(e.items()):

        lim = v//u

        for x in range(-1 - lim, 1 - lim):

            t = v + x * u

            d[t] = min(c + i * abs(x), d.get(t, 999))

print(d[0])



# Made By Mostafa_Khaled


class OneArithmetic():
    def __init__(self, n):
        self.n = n
        self.m = [0]*17
        v = 0
        for i in range(1,17):
            v = 10*v+1
            self.m[i] = v

    def dfs(self, v, d):
        v = abs(v)
        if d == 1:
            return v
        elif v == 0:
            return 0
        else:
            k = int(v/self.m[d])
            ans = min( self.dfs( v - k * self.m[d] , d - 1 ) + k * d , self.dfs( ( k + 1 ) * self.m[d] - v , d - 1 ) + ( k + 1 ) * d )
            return ans

    def min_digits(self):
        v =  self.dfs(self.n,16)
        return v


n = int(input())
print(OneArithmetic(n).min_digits())