In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.

-----Constraints-----
 - N is an integer between 1 and 10^{1,000,000} (inclusive).

-----Input-----
Input is given from Standard Input in the following format:
N

-----Output-----
Print the minimum possible number of total banknotes used by you and the clerk.

-----Sample Input-----
36

-----Sample Output-----
8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Difficulty: interview

Starter code:

Solutions:
s=input()
dp0=[0]*(len(s)+1)
dp1=[0]*(len(s)+1)
dp1[0]=1
for i in range(1,len(s)+1):
  n=int(s[i-1:i])
  dp0[i]=min(dp0[i-1]+n,dp1[i-1]+10-n)
  dp1[i]=min(dp0[i-1]+(1 if n+1==10 else n+1),dp1[i-1]+10-n-1)
print(dp0[-1])

N = input()
dp = [[0]*2 for _ in range(1001000)]
dp[0][1] = 1
for i in range(len(N)):
    d = int(N[i])
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d)
    dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1)  # もらいすぎている状態を維持する
ans = dp[len(N)][0]
print(ans)


p, q = 0, 1
for x in map(int, input()):
    p, q = min(p + x, q + 10 - x), min(p + x + 1, q + 9 - x)
print(p)


S=input()[::-1]
ans=0
#支払う枚数
array = list(map(int, S))
L=len(array)
for i,n in enumerate(array):
    if n< 5 :
        ans+=n
    elif n > 5:
        ans+= 10-n
        if i <L-1:
            array[i+1] +=1
        else:
            ans +=1
    else:
        ans+=5
        if i < L-1:
            if array[i+1] >=5:
                array[i+1] +=1
                
print( ans)


import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = input()
    count = 0

    S = len(N)
    flag = 0
    for i in range(S):
        temp = flag + int(N[-1 - i])
        if temp > 5:
            count += 10 - temp
            flag = 1
        elif temp ==5  and i !=S-1 and int(N[-1-i-1])>=5:
            count+=temp
            flag=1

        else:
            count += temp
            flag = 0

    print((count + flag))


def __starting_point():
    main()

__starting_point()

n=input()
k=len(n)
dp=[[0]*2 for _ in range(k)]
dp[0][0]=min(int(n[0]),11-int(n[0]))
dp[0][1]=min(int(n[0])+1,11-(int(n[0])+1))
for i in range(1,k):
  dig=int(n[i])
  dp[i][0]=min(dp[i-1][0]+dig,dp[i-1][1]+10-dig)
  dp[i][1]=min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1))
print(dp[k-1][0])

S = input()
L = len(S)

dp = [[0 for _ in range(L + 1)] for over in range(2)]
dp[0][0] = 0
dp[1][0] = 1

for j, s in enumerate(S, 1):
    n = int(s)
    dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n))
    dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1))
    
print(dp[0][-1])

N=input()
N=N[::-1]+"0"
ans=0
up=0
for i,n in enumerate(N):
  d=int(n)+up
  if d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5):
    ans+=(10-d)
    up=1
  else:
    ans+=d
    up=0
print(ans)

def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve()

def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print(( min(dp[N][0], dp[N][1])))
def __starting_point():
    main()

__starting_point()

N = [int(_) for _ in list(input())]
a, b = 0, 1
for n in N:
    a, b = min(a + n, b + 10 - n), min(a + (n + 1), b + 10 - (n + 1))
print(a)


n = [int(i) for i in list(input())]
a, b = 0, 1
for i in n:
    a, b = min(a+i, b+10-i), min(a+(i+1), b+10-(i+1))
print(a)


INF = float("inf")

def main():
	n = list(map(int, list(input())))
	n.reverse()
	# print(n)
	n.append(0)
	N = len(n) - 1
	DP = [[INF, INF] for i in range(N + 2)]
	DP[-1][1] = 0
	for i in range(N + 1):
		DP[i][0] = min(DP[i - 1][0] + (9 - n[i]), DP[i - 1][1] + (10 - n[i]))
		DP[i][1] = min(DP[i - 1][0] + n[i] + 1, DP[i - 1][1] + n[i])

	ans = min(DP[-2])
	# print(DP)
	print(ans)

def __starting_point():
	main()
__starting_point()

N = input()

dp0 = 0  
dp1 = 1  
 
for n in N:
    n = int(n)
    dp0_new = min(dp0 + n, dp1 + (10 - n))
    n = n + 1 #繰り上げさせてる
    dp1_new = min(dp0 + n, dp1 + (10 - n))
    dp0, dp1 = dp0_new, dp1_new

print(dp0)

N = input()
dp = [[0]*2 for _ in range(1001000)]
dp[0][1] = 1
for i in range(len(N)):
    d = int(N[i])
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d)
    dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1)  # もらいすぎている状態を維持する
ans = dp[len(N)][0]
print(ans)


n = list(map(int, list(input())))
ln = len(n)
ans = 0
for i in range(-1, -ln, -1):
    if n[i] == 10:
        n[i-1] += 1
        continue
    elif n[i] < 5:
        ans += n[i]
    elif n[i] > 5:
        ans += 10 - n[i]
        n[i-1] += 1
    else:
        if n[i-1] < 5:
            ans += 5
        else:
            ans += 5
            n[i-1] += 1
if n[0] == 10:
    ans += 1
elif n[0] <= 5:
    ans += n[0]
else:
    ans += 11 - n[0]

print(ans)

S = input()
L = len(S)

INF = float('inf')
dp = [[INF]*(L+1) for _ in range(2)]
dp[0][0] = 0

for i in range(L):
    d = int(S[-1-i])
    dp[0][i+1] = min(dp[0][i], dp[1][i]) + d
    dp[1][i+1] = min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1)

print(min(dp[0][-1], dp[1][-1]))

N = [int(s) for s in input()]
M = len(N)

eq = 0
ov = 1

for n in N:
    a = min(eq + n, ov + 10 - n)
    b = min(eq + (n + 1), ov + 10 - (n + 1))
    eq, ov = a, b

print(eq)


n = str(input())
n_list = list(reversed(n))
n_list.append('0')
sum = 0
kuriagari = False
for i in range(len(n_list)):
    num = int(n_list[i])
    if(kuriagari):
        num+=1
        kuriagari=False
    
    if(num==10):
        num=0
        kuriagari=True

    if(num<5):
        sum+=num
    else:
        if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True

print(sum)





def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print((dp[n][0]))


def __starting_point():
    resolve()

__starting_point()

def main():
    import sys
    input = sys.stdin.readline

    N = list(map(int, list(input()[:-1])))
    N = N[::-1] + [0]

    ans = 0
    for i in range(len(N)-1):
        if N[i] < 5:
            ans += N[i]
        elif N[i] > 5:
            ans += 10 - N[i]
            N[i+1] += 1
        else:
            ans += 5
            if N[i+1] >= 5:
                N[i+1] += 1

    print(ans + N[len(N)-1])

main()

def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans

def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}")
def main():
    N = list(map(int, input()))
    print((solve(N)))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans)
main()


N=input()
inf=float("inf")
dp=[[inf]*(len(N)+1) for i in range(2)]
dp[0][0]=0
for i in range(len(N)):
   tmp=int(N[-1-i])
   dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp
   dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp
print(min(dp[0][-1],dp[1][-1]))

S = input()
just, over = 0, 1
for c in S:
    c = int(c)
    just, over = [
        min(just + c, over + (10-c)),
        min(just + (c+1), over + (9-c))
    ]
print(just)


digits = input()
digits = list(map(int, digits[::-1]))
digits.append(0)

ans = 0
for i in range(len(digits)):
    if digits[i] < 5:
        ans += digits[i]
    elif digits[i] == 5:
        if digits[i + 1] < 5:
            ans += digits[i]
        else:
            digits[i + 1] += 1
            ans += 10 - digits[i]
    else:
        # digits[i] > 5
        digits[i + 1] += 1
        ans += 10 - digits[i]

print(ans)


N = list(map(int, input()))

# print(N)
ans = 0
DP = [0] *2
DP[0] = 0
DP[1] = 1
for i in N:
    a, b = DP[0] ,DP[1]
    DP[0] = a + i if a+i < b +(10-i) else b +(10-i)
    DP[1] = a + i+1 if a + i+1 < b + (9-i) else b + (9-i)

    # print(DP)

print(DP[0] if DP[0] <= DP[1]+1 else DP[1] +1)

n = "0" + input() # 文字列として受け取り
N = len(n)
plus = 1
just = 0
for i in range(1,N):
    #前の桁がplusだった場合の最低枚数を計算する
    plus_plus = plus+10-(int(n[i])+1)#この桁も１枚多く払う
    plus_just = plus+10-int(n[i])#この桁はちょうど払う
    #前の桁はちょうど払っていた場合
    just_plus = just+1+int(n[i])#この桁は１枚多く払う
    just_just = just+int(n[i])#この桁もちょうど払う
    
    plus = min(plus_plus,just_plus)
    just = min(just_just,plus_just)

#最後の桁は1枚余分に払う必要がない
print(just)

n=[int(x) for x in list(input()[::-1])]+[0]
ans = 0
step = 0
for i in range(len(n)-1):
  x = n[i]+step
  if x < 5 or (x==5 and n[i+1]<5):
    ans += x
    step = 0
  else:
    ans += 10 - x
    step = 1
print(ans+step)

N = input()[::-1]
l = len(N)
dp = [[0,0] for i in range(l+1)]

for i in range(l):
    dp[i+1][0] = min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1)
    if i == 0:
        dp[i+1][1] = 10 - int(N[i])
    else:
        dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i]))

print((min(dp[-1][0],dp[-1][1]+1)))


#!/usr/bin python3
# -*- coding: utf-8 -*-

N = list(map(int, list(input())))
N.reverse()
N=N+[0]
cnt = 0
for i in range(len(N)):
    n=N[i]
    if n<5:
        cnt+=n
    elif n==5:
        cnt+=n
        if N[i+1]>=5:
            N[i+1]+=1
    else:
        cnt+=10-n
        N[i+1]+=1
print(cnt)


s = input()
n = len(s)

ans = 0
dp1 = [0]*(n+1)
dp0 = [0]*(n+1)

dp1[0] = 1

for i in range(n):
  k = int(s[i])
  if k < 5:
    dp0[i+1] = dp0[i] + k
  elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
  k += 1
  if k == 10:
    dp1[i+1] = dp1[i]
  elif k < 5:
    dp1[i+1] = dp0[i] + k
  elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k

print(dp0[n])

n = input().rstrip()

total = 0
carry = 0
for i in range(len(n)-1, -1, -1):
    v = int(n[i]) + carry
    if v > 5:
        total += 10 - v
        carry = 1
    elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
    else:
        total += v
        carry = 0

print(total + carry)

import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = list(map(int,input()))
    count = 0

    S = len(N)
    flag = 0
    for i in range(S):
        temp = flag + N[-1 - i]
        if temp > 5:
            count += 10 - temp
            flag = 1
        elif temp ==5  and i !=S-1 and N[-1-i-1]>=5:
            count+=temp
            flag=1

        else:
            count += temp
            flag = 0

    print((count + flag))


def __starting_point():
    main()

__starting_point()

def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a)

main()


s = input()[::-1]
size = len(s)
s += "4"

ans = 0
bef = 0

for i in range(size):
    v1 = int(s[i])
    v2 = int(s[i+1])
    if v1+bef>=6 or (v1+bef>=5 and v2>=5):
        ans += 10-(v1+bef)
        bef = 1
    else:
        ans += (v1+bef)
        bef = 0

ans += bef
print(ans)

N = [0] + list(map(int, list(input())))
L = len(N)
ans = 0
for i in range(L-1, -1, -1):
    if N[i] == 10:
        if i == 0:
            ans += 1
        else:
            N[i-1] += 1
            N[i] = 0
    if N[i] < 5:
        ans += N[i]
    elif N[i] > 5:
        ans += 10 - N[i]
        N[i-1] += 1
    else:
        if i == 0:
            ans += N[i]
        else:
            if N[i-1] >= 5:
                N[i-1] += 1
                ans += 5
            else:
                ans += N[i]

print(ans)


def main():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]

    ans = 0
    for i in range(len(N)-1):
        if N[i] < 5:
            ans += N[i]
        elif N[i] > 5:
            ans += 10 - N[i]
            N[i+1] += 1
        else:
            ans += 5
            if N[i+1] >= 5:
                N[i+1] += 1

    print(ans + N[len(N)-1])

main()

def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
      
def __starting_point():
    main()
__starting_point()

def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [min(dp[0] + x, dp[1] + (10-x)), min(dp[0] + x+1, dp[1] + (10-x) - 1)]
    print((dp[0]))

def __starting_point():
    main()

__starting_point()

s = '0' + input()
m = len(s)
up = 0
cnt = 0
y = []
for i in range(-1, -m-1, -1):
    cs = int(s[i]) + up
    if cs < 5 or (cs == 5 and int(s[i-1]) < 5):
        cnt += cs
        y.append([cs, s[i]])
        up = 0
    else:
        cnt += 10 - cs
        y.append([10 - cs, s[i]])
        up = 1
        
print(cnt)

# https://atcoder.jp/contests/abc155/submissions/10157837
# 写経

def main():
    a = [0]
    a += list(map(int, input()))
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt)


def __starting_point():
    main()

__starting_point()

# coding: utf-8


def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a)


def __starting_point():
    print((solve(*(open(0).read().splitlines()))))

__starting_point()

n = input()
x = len(n)
inf = 10 ** 9
#dp[i][flg]...i桁目まで、未満フラグ=1
dp = [[inf] * 2 for _ in range(x+1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(x):
    s = int(n[i])
    dp[i+1][0] = min(dp[i][0] + s, dp[i][1] + 10 - s)
    dp[i+1][1] = min(dp[i][0] + s + 1, dp[i][1] + 9 - s)

print((dp[x][0]))


n='0'+input()

X=[]
for i in range(len(n)):
    X.append(int(n[i]))
cnt=0
i=len(n)-1
while i>=0:
    if X[i]>5:
        cnt+=10-X[i]
        X[i-1]+=1
        
    elif X[i]==5:
        cnt+=5
        if X[i-1]>=5:
            X[i-1]+=1
    else:
        cnt+=X[i]
    i-=1
print(cnt)


N = list(map(int, list(input())))[::-1]
L = len(N)

answer = 0
for i, n in enumerate(N):
    if n < 5:
        answer += n
    elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
    else:
        answer += 5
        if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1

print(answer)

N = list(input())

num = 0
ans = 0
for i in range(len(N)-1, -1, -1):
    n = int(N[i])
    if n+num == 5 and i > 0 and int(N[i-1]) >= 5:
        ans += 5
        num = 1
    elif n+num < 6:
        ans += n+num
        num = 0
    else:
        ans += 10-(n+num)
        num = 1

ans += num
print(ans)

N = list(map(int, input()))

ans = 0
carry = 0   #0 or 1 or 2. when 2, it means that it's optional between 0 and 1.
for i in range(len(N) - 1, -1, -1):
    if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
        

if carry == 1:
    ans += carry

print(ans)

def main():
    *e, = list(map(int, input()))
    e.reverse()

    inf = 20 * len(e)

    dp = [0, inf]  # 繰り下がり無,有

    for x in e:
        ndp = [-1, -1]  # 繰り下がり無,有

        ndp[0] = min(
            dp[0] + x,
            dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf
        )  # ちょうど支払う

        ndp[1] = min(
            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf
            dp[1] + (10 - (x + 1))
        )  # 0枚支払って繰り下がり

        dp = ndp

    print((min(dp[0], dp[1] + 1)))


def __starting_point():
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

__starting_point()

n=list(map(int,input()))
keta=len(n)
satsu=0

for i in range(keta):
    tmp = n.pop()
    if i == keta-1:
        n.append(0)
    
    if tmp == 10:
        tmp=0
        n[-1]=n[-1]+1
    if tmp > 5 or (tmp==5 and n[-1] >= 5):
        satsu += 10 - tmp
        n[-1] = n[-1] + 1
    else:
        satsu += tmp

print((satsu+n[0]))


import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    N = input() + "0"

    dp = [[f_inf] * 2 for _ in range(len(N) + 1)]
    dp[0][0] = 0
    dp[0][1] = 1
    for i in range(1, len(N) + 1):
        n = int(N[i - 1])
        dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (10 - n))
        dp[i][1] = min(dp[i - 1][0] + n + 1, dp[i - 1][1] + (10 - n - 1))

    print((dp[-1][0]))


def __starting_point():
    resolve()

__starting_point()

n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
        n_i = int(n[i])
        dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
        dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1]))

def main():
    N = "0" + input()
    N = list(map(int, N[::-1]))
    ans = 0
    up = 0
    for i, n in enumerate(N):
        d = n + up
        # くりあがりアリ
        if d > 5 or (i < len(N) - 1 and d == 5 and N[i + 1] >= 5):
            ans += 10 - d  # 0枚の支払い（後に回す）、10-d枚のおつり
            up = 1
        # くりあがりナシ
        else:
            ans += d  # d枚の支払い、0枚のおつり
            up = 0
    print(ans)


def __starting_point():
    main()
__starting_point()


def resolve():
    S = "0" + input()
    N = len(S)

    dp = [[1<<60]*2 for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        now = int(S[i])
        dp[i+1][0] = min(dp[i][0]+now, dp[i][1]+(10- now))
        dp[i + 1][1] = min(dp[i][0] + now + 1, dp[i][1] + (10 - now - 1))
    print((dp[N][0]))


def __starting_point():
    resolve()

__starting_point()

n = input()[::-1]
dp = [[0, 0] for i in range(len(n) + 1)]
dp[0][1] = 1
for i in range(len(n)):
    dp[i + 1][0] = min(dp[i][0] + int(n[i]), dp[i][1] - int(n[i]) + 10)
    dp[i + 1][1] = min(dp[i][0] + int(n[i]) + 1, dp[i][1] - int(n[i]) + 9)
print(dp[len(n)][0])

N = (list(input()))
N =N[::-1]
N_int = [int(i) for i in N]
N_int.append(0)
maisu = 0
keta = False
for i in range(len(N_int)-1):
    if keta ==True:
        N_int[i] +=1
    if N_int[i]<5:
        maisu +=N_int[i]
        keta =False
    elif N_int[i]==5 :
        if N_int[i+1]>4:
            keta =True
            maisu +=(10-N_int[i])
        else:
            keta =False
            maisu += N_int[i]
    else:
        keta =True
        maisu +=(10-N_int[i])  
if keta ==True:
    maisu +=1
print(maisu)

N = [int(_) for _ in list(input())]
a, b = 0, 1
for n in N:
    a, b = min(a+n, b+10-n), min(a+(n+1), b+10-(n+1))
print(a)


import numpy as np
from numba import njit

N = np.array([int(_) for _ in input()])


def solve(N):
    p, q = 0, 1
    for x in N:
        p, q = min(p + x, q + 10 - x), min(p + x + 1, q + 9 - x)
    print(p)


solve(N)


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0]

print(main())





s = input()
l = list(s)
l = [int(x) for x in l]
dp = [[0, 0] for i in range(len(s))]
dp[0][0] = l[0]
dp[0][1] = 11 - l[0]
for i in range(1, len(s)):
  dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + l[i]
  dp[i][1] = min(dp[i - 1][0] + 11 - l[i], dp[i - 1][1] + 9 - l[i])
print((min(dp[-1][0], dp[-1][1])))


#

import sys
input=sys.stdin.readline

def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print((min(dp[-1])))
    
def __starting_point():
    main()

__starting_point()

N = [int(_) for _ in input()]
dp = [[0, 0] for _ in range(len(N))]
dp[0][0] = min(N[0], 11 - N[0])
dp[0][1] = min(N[0] + 1, 10 - N[0])
for i in range(1, len(N)):
    dp[i][0] = min(dp[i - 1][0] + N[i], dp[i - 1][1] + 10 - N[i])
    dp[i][1] = min(dp[i - 1][0] + N[i] + 1, dp[i - 1][1] + 9 - N[i])
print((dp[-1][0]))


x = list(map(int,list(input())))
n = len(x)
dp = [[0,1] for _ in range(n+1)]
for i in range(n):
    dp[i+1][0] = min(dp[i][0]+x[i],dp[i][1]+10-x[i])
    dp[i+1][1] = min(dp[i][0]+x[i]+1,dp[i][1]+9-x[i])
print((dp[n][0]))


#E
#greedy 
n = input()
n = n[::-1] + "0"
l = len(n)

ans = 0

kuriagari=0

for i,v in enumerate(n):
    v = int(v)
    if kuriagari:
        v += 1
    
    if v<5:
        ans += v
        kuriagari = 0
    if v>5:
        ans += 10 - v
        kuriagari = 1
    if v==5:
        if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
        if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
        
print(ans)

# import time
N = input()
n = len(N)
# t1 = time.time()
# ぴったり払う:a 余分に払う:b
a = int(N[0])
b = 11-int(N[0])
# t2 = time.time()
for i in range(n-1):
    a1 = min(a+int(N[i+1]), b+int(N[i+1]))
    b1 = min(a+11-int(N[i+1]), b+9-int(N[i+1]))
    a = a1
    b = b1
print((min(a,b)))
# t3 = time.time()

# print(t2-t1,t3-t2)


L = list(map(int,list(input())))
L = [0] + L
N = len(L)
dp1 = [0 for _ in range(N+1)]
dp2 = [0 for _ in range(N+1)]

for i in range(N):
    dp1[i+1] = min(dp1[i] + L[i], dp2[i] + 10 - L[i])
    dp2[i+1] = min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1)

print(dp1[-1])

s = input()
s = s[::-1]
s += '0'
ans = 0
dp = [0, 10**9]
for c in s:
    i = int(c)
    a = dp[0]+i
    if i<9 and a>dp[1]+i+1:
        a = dp[1]+i+1
    b = dp[1]+9-i
    if i>0 and b>dp[0]+10-i:
        b = dp[0]+10-i
    dp = [a, b]
print(dp[0])

N = [int(s) for s in input()]
n = len(N)
a = 0
b = 11
for n in N:
    a1 = min(a+n, b+n)
    b1 = min(a+11-n, b+9-n)
    a = a1
    b = b1
print(min(a,b))

n = input()
a = 0
b = 2
for s in n:
  v = int(s)
  a_ = min(a + v, b + v)
  b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
  a = a_
  b = b_
  
print(min(a, b))

s = input()
n = len(s)

ans = 0

sub = 0
judge = False
for i in range(n-1, -1, -1):
  key = int(s[i])+sub
  if key <= 4:
    ans += key
    sub = 0
  elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
  else:
    ans += 10-key
    sub = 1
ans += sub
print(ans)

s = input()
state = [0, 1]
for i in s:
    n = int(i)
    state_new = [min(state[0] + n, state[1] + 10 - n), 
                 min(state[0] + n + 1, state[1] + 9 - n)]
    state = state_new
print(state[0])

n = input()
len_n = len(n)
dp = [[0,2] for i in range(len_n+1)]
for k,v in enumerate(n[::-1]):
    v = int(v)
    dp[k+1][0] = min(dp[k][0] + v, dp[k][1] + v)
    dp[k+1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1)
print((min(dp[len_n])))


N = input()
n = len(N)
a = int(N[0])
b = 11-int(N[0])
for i in range(n-1):
    a1 = min(a+int(N[i+1]), b+int(N[i+1]))
    b1 = min(a+11-int(N[i+1]), b+9-int(N[i+1]))
    a = a1
    b = b1
print(min(a,b))

N = input()
X = list(map(int, list(str(N))))

dp1 = [float('inf')] * (len(X) + 1)  # dp1[i] := 上からi番目の金額を丁度払う場合の最小値
dp2 = [float('inf')] * (len(X) + 1)  # dp2[i] := 上からi番目の金額を１周り大きい額で支払う場合の最小値
dp1[0] = 0
for i, x in enumerate(X):
    dp1[i + 1] = min(dp1[i] + x, dp2[i] + x)
    dp2[i + 1] = min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1)

print((min(dp1[-1], dp2[-1])))


N = list(map(int, input()))

m = 0 # 最小の枚数
m_ = 1 # 繰り上がり時の枚数

for n in N:
  m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1))
print(m)

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = sr()
dp = [0, 1]  # ちょうど払う最小枚数、１円多くはらう最小枚数
for x in N:
    x = int(x)
    prev = dp[:]
    dp[0] = min(prev[0]+x, prev[1]+(10-x))
    dp[1] = min(prev[0]+x+1, prev[1]+(10-x-1))

answer = dp[0]
print(answer)
# 33


n = input()
dp = [0,1]
ans = 0
for i in range(len(n)):
    cur1 = dp[0]
    cur2 = dp[1]
    if i != len(n)-1:
        dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i]))
        dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i]))
    else:
        ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i]))
print(ans)


N = [int(c) for c in input()][::-1]
N.append(0)
ans = 0
for i in range(len(N)):
    c = N[i]
    if c <= 4:
        ans += c
    elif 6 <= c:
        ans += 10 - c
        N[i + 1] += 1
    else:
        if N[i + 1] < 5:
            ans += c
        else:
            ans += 10 - c
            N[i + 1] += 1
print(ans)

# coding: utf-8
import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**7)

def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count)

def __starting_point():
    run()
__starting_point()

A = list(map(int, input()))
while A[-1] == 0:
  A.pop()
n = len(A)
dp = [[0]*2 for _ in range(n)]
dp[0][1] = 1
for i, a in enumerate(A[:-1]):
  if i < n-1:
    dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a)
    dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a)
ans = min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1])
print(ans)

#E
#greedy 
n = input()
l = len(n)

#a is kuriagari naku, pittari na case
#b is kuriagari ari
a,b = 0,1
for v in n:
    v = int(v)
    a_next = min(a+v, b+(10-v))
    b_next = min(a+v+1, b+(10-(v+1)))
    a = a_next
    b = b_next
    
    #繰り上がりが発生する⇔引く数を+1する
    #(10-v)や(10-(v+1)) means charges
    #last keta is pittari, no amari happenes

print(a)

n=[int(x) for x in input()]

dp=[[100000000000 for i in range(len(n)+1)]for j in range(2)]
dp[0][0]=0
dp[1][0]=1
for i in range(len(n)):
  d=n[i]
  dp[0][i+1]=min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i])
  dp[1][i+1]=min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i])
  
  
print((dp[0][len(n)]))
#print(dp)


s = "0"+input()
ans = 0
n = len(s)
f = 0
p = 0
for i in range(1,n+1):
    n = int(s[-i])
    # n += f
    if p+(n>4) > 5:
        f = 1
    else:
        # ans += f
        f = 0
    n += f
    ans += min(n,10-n)
    p = n
# ans += f
print(ans)

s = str(input())
n = len(s)
dp = [[0]*2 for _ in range(n+1)]
dp[0][1] = 1
for i in range(n):
    p = int(s[i])
    dp[i+1][0] = min(dp[i][1]+10-p,dp[i][0]+p)
    dp[i+1][1] = min(dp[i][1]+10-p-1,dp[i][0]+p+1)
print(dp[-1][0])

N = input()
N = N[::-1]+"0"
ans = 0
up = 0
for i,n in enumerate(N):
    d = int(n)+up
    if 5 < d or (d == 5 and i < len(N)-1 and int(N[i+1]) >= 5):
        ans += (10-d)
        up = 1
    else:
        ans += d
        up = 0
print(ans)


N=input()

dp0=[0]*(len(N)+1)
dp1=[0]*(len(N)+1)
dp1[0]=1

for i in range(len(N)):
    n=int(N[i])
    dp0[i+1]=min(dp0[i]+n, dp1[i]+(10-n))
    n+=1
    dp1[i+1]=min(dp0[i]+n, dp1[i]+(10-n))

#print(dp0)
#print(dp1)
print((dp0[-1]))


import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()


n = ns()
n = list(map(int,n[::-1])) + [0]

ans = 0

for i in range(len(n)):
    a = n[i]
    if a <= 4:
        ans += a
    elif a >= 6:
        ans += 10-a
        n[i+1] += 1
    else:
        if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1

print(ans)

s = input()
n = len(s)

s = list(map(int, list(s)))
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
dp2[0] = 1

for k in range(n):
    dp1[k + 1] = min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]])
    dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1])

print(dp1[-1])

N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
    
print(count)

def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print((min(dp)))

def __starting_point():
    main()

__starting_point()

N = input()
a, b = 0, 1
for i in N:
    x = int(i)
    a, b = min(a+x, b+10-x), min(a+x+1, b+9-x)
print (a)

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n = list(input().rstrip())
    n = [0] + n
    n = n[::-1]
    ln = len(n)
    res = 0
    a = 0
    for i in range(ln):
        s = int(n[i])
        s += a
        if 0<=s<=4:
            res += s
            a = 0
        elif 6<=s:
            res += 10-s
            a = 1
        else:
            if int(n[i+1])>=5:
                res += 10-s
                a = 1
            else:
                res += s
                a = 0
            
    print(res)

def __starting_point():
    main()
__starting_point()

S = ['0'] + list(input()) + ['0']
N = len(S)
flag = False

ans = 0

for i in range(N - 1, 0, -1):
    j = int(S[i])
    if flag:
        j += 1
        S[i] = j

    if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

    else:
        ans += 10 - j
        flag = True

if flag:
    ans += 1


# print (ans)
count = 0
for s in S:
    if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0

print (ans)
            


        
    


S = input()
ans = 0
flag = False #繰り上がりありならTrue
flag2 = False #前が5ならTrue
for k in range(len(S)):
  n = int(S[-k-1])
  if flag2:
    if n < 5:
      flag = False
    else:
      flag = True
  if flag:
    n += 1
  if n == 5:
    ans += 5
    flag2 = True
    flag = False
  elif n < 5:
    ans += n
    flag2 = False
    flag = False
  else:
    ans += 10-n
    flag2 = False
    flag = True
if flag:
  ans += 1
print(ans)

N = input()

over = 1
eq = 0

for n in map(int, N):
    o = min(eq + n + 1, over + (10 - n) - 1)
    e = min(eq + n, over + (10 - n))

    over = o
    eq = e

print(eq)