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


if __name__ == '__main__':
    resolve() import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 f_inf = float('inf') f_inf float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 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]) resolve N = input() + "0" N input() + "0" input() input "0" [f_inf] * 2 for _ in range(len(N) + 1) for _ in range(len(N) + 1) _ range(len(N) + 1) range len(N) + 1 len(N) len N 1 for _ in range(len(N) + 1) [f_inf] * 2 [f_inf] f_inf 2 dp = [[f_inf] * 2 for _ in range(len(N) + 1)] dp [[f_inf] * 2 for _ in range(len(N) + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[0][1] = 1 dp[0][1] [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))

     i range(1, len(N) + 1) range 1 len(N) + 1 len(N) len N 1 n = int(N[i - 1]) n int(N[i - 1]) int N[i - 1] N i - 1 i 1 dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (10 - n)) dp[i][0] [i] dp i 0 min(dp[i - 1][0] + n, dp[i - 1][1] + (10 - n)) min dp[i - 1][0] + n dp[i - 1][0] [i - 1] dp i - 1 i 1 0 n dp[i - 1][1] + (10 - n) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - n 10 n dp[i][1] = min(dp[i - 1][0] + n + 1, dp[i - 1][1] + (10 - n - 1)) dp[i][1] [i] dp i 1 min(dp[i - 1][0] + n + 1, dp[i - 1][1] + (10 - n - 1)) min dp[i - 1][0] + n + 1 dp[i - 1][0] + n dp[i - 1][0] [i - 1] dp i - 1 i 1 0 n 1 dp[i - 1][1] + (10 - n - 1) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - n - 1 10 - n 10 n 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 if __name__ == '__main__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve mod = 10 ** 9 + 7 10 ** 9 + 7 mod 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 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]) resolve f_inf = float('inf') float('inf') f_inf
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])


if __name__ == '__main__':
    resolve()
