def dp_solver(tmp):

    l=len(tmp)

    rev=tmp[::-1]+"0"

    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]

    dp[0][0]=0

    for i in range(l+1):

        for j in range(2):

            num=int(rev[i])

            num+=j

            if num<10:

                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)

            if num>0:

                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))



    #import numpy as np

    #dp=np.array(dp)

    return(dp[l+1])



s=eval(input())

ans=dp_solver(s)

print((ans[0]))
###さらに綺麗に！！！

def y_solver(tmp):

    l=len(tmp)

    rev=[int(i) for i in tmp[::-1]+"0"]

    ans=0

    for i in range(l):

        num=rev[i]

        next_num=rev[i+1]

        if (num>5) or (num==5 and next_num>=5):

            rev[i+1]+=1

        ans+=min(num,10-num)  

    last=rev[l]

    ans+=min(last,10-last)

    return ans

  

s=eval(input())

ans=y_solver(s)

print(ans)
def dp_solver(tmp):
    l = len(tmp)
    rev = tmp[::-1] + "0"
    dp = [[float("inf") for _ in range(2)] for _ in range(l + 2)]
    dp[0][0] = 0
    for i in range(l + 1):
        for j in range(2):
            num = int(rev[i])
            num += j
            if num < 10:
                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + num)
            if num > 0:
                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - num))
    # import numpy as np
    # dp=np.array(dp)
    return dp[l + 1]


s = eval(input())
ans = dp_solver(s)
print((ans[0]))

###さらに綺麗に！！！
def y_solver(tmp):
    l = len(tmp)
    rev = [int(i) for i in tmp[::-1] + "0"]
    ans = 0
    for i in range(l):
        num = rev[i]
        next_num = rev[i + 1]
        if (num > 5) or (num == 5 and next_num >= 5):
            rev[i + 1] += 1
        ans += min(num, 10 - num)
    last = rev[l]
    ans += min(last, 10 - last)
    return ans


s = eval(input())
ans = y_solver(s)
print(ans)

-def dp_solver(tmp): +###さらに綺麗に！！！ +def y_solver(tmp): -    rev = tmp[::-1] + "0" -    dp = [[float("inf") for _ in range(2)] for _ in range(l + 2)] -    dp[0][0] = 0 -    for i in range(l + 1): -        for j in range(2): -            num = int(rev[i]) -            num += j -            if num < 10: -                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + num) -            if num > 0: -                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - num)) -    # import numpy as np -    # dp=np.array(dp) -    return dp[l + 1] +    rev = [int(i) for i in tmp[::-1] + "0"] +    ans = 0 +    for i in range(l): +        num = rev[i] +        next_num = rev[i + 1] +        if (num > 5) or (num == 5 and next_num >= 5): +            rev[i + 1] += 1 +        ans += min(num, 10 - num) +    last = rev[l] +    ans += min(last, 10 - last) +    return ans -ans = dp_solver(s) -print((ans[0])) +ans = y_solver(s) +print(ans)