nl = list(input())


# 繰り上げと繰り下げ
dp = [[0, 10000] for i in range(len(nl) + 1)]

for i, n in enumerate(nl[::-1]):
    n = int(n)
    dp[i+1][0] = min(dp[i][0] + n, dp[i][1] + n + 1)
    dp[i+1][1] = min(10 - n + dp[i][0], 10 - n - 1 + dp[i][1])



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


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


# 繰り上げと繰り下げ
dp = [[0, 10000] for i in range(len(nl) + 1)]

for i, n in enumerate(nl[::-1]):
    n = int(n)
    dp[i+1][0] = min(dp[i][0] + n, dp[i][1] + n + 1)
    dp[i+1][1] = min(10 - n + dp[i][0], 10 - n - 1 + dp[i][1])



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



