import sys



n = sys.stdin.readline().rstrip()

l = len(n)



def main():

    dp0 = [None] * (l + 1)

    dp1 = [None] * (l + 1)

    dp0[0] = 0

    dp1[0] = 1



    for i in range(l):

        d = int(n[i])

        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)

        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)

    

    return dp0[l]



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys



n = sys.stdin.readline().rstrip()



def main():

    dp = [0, 1]

    for d in n:

        d = int(d)

        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)



    return dp[0]



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys

n = sys.stdin.readline().rstrip()
l = len(n)


def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1
    for i in range(l):
        d = int(n[i])
        dp0[i + 1] = min(dp0[i] + d, dp1[i] + 10 - d)
        dp1[i + 1] = min(dp0[i] + d + 1, dp1[i] + 10 - d - 1)
    return dp0[l]


if __name__ == "__main__":
    ans = main()
    print(ans)

import sys

n = sys.stdin.readline().rstrip()


def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0] + d, dp[1] + 10 - d), min(
            dp[0] + d + 1, dp[1] + 10 - d - 1
        )
    return dp[0]


if __name__ == "__main__":
    ans = main()
    print(ans)

-l = len(n) -    dp0 = [None] * (l + 1) -    dp1 = [None] * (l + 1) -    dp0[0] = 0 -    dp1[0] = 1 -    for i in range(l): -        d = int(n[i]) -        dp0[i + 1] = min(dp0[i] + d, dp1[i] + 10 - d) -        dp1[i + 1] = min(dp0[i] + d + 1, dp1[i] + 10 - d - 1) -    return dp0[l] +    dp = [0, 1] +    for d in n: +        d = int(d) +        dp[0], dp[1] = min(dp[0] + d, dp[1] + 10 - d), min( +            dp[0] + d + 1, dp[1] + 10 - d - 1 +        ) +    return dp[0]