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

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

main()
