n = input()
x = len(n)
inf = 10 ** 9
#dp[i][flg]...i桁目まで、未満フラグ=1
dp = [[inf] * 2 for _ in range(x+1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(x):
    s = int(n[i])
    dp[i+1][0] = min(dp[i][0] + s, dp[i][1] + 10 - s)
    dp[i+1][1] = min(dp[i][0] + s + 1, dp[i][1] + 9 - s)

print(dp[x][0]) n = input() n input() input x = len(n) x len(n) len n inf = 10 ** 9 inf 10 ** 9 10 9 [inf] * 2 for _ in range(x+1) for _ in range(x+1) _ range(x+1) range x+1 x 1 for _ in range(x+1) [inf] * 2 [inf] inf 2 dp = [[inf] * 2 for _ in range(x+1)] dp [[inf] * 2 for _ in range(x+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(x):
    s = int(n[i])
    dp[i+1][0] = min(dp[i][0] + s, dp[i][1] + 10 - s)
    dp[i+1][1] = min(dp[i][0] + s + 1, dp[i][1] + 9 - s) i range(x) range x s = int(n[i]) s int(n[i]) int n[i] n i dp[i+1][0] = min(dp[i][0] + s, dp[i][1] + 10 - s) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + s, dp[i][1] + 10 - s) min dp[i][0] + s dp[i][0] [i] dp i 0 s dp[i][1] + 10 - s dp[i][1] + 10 dp[i][1] [i] dp i 1 10 s dp[i+1][1] = min(dp[i][0] + s + 1, dp[i][1] + 9 - s) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + s + 1, dp[i][1] + 9 - s) min dp[i][0] + s + 1 dp[i][0] + s dp[i][0] [i] dp i 0 s 1 dp[i][1] + 9 - s dp[i][1] + 9 dp[i][1] [i] dp i 1 9 s print(dp[x][0]) print dp[x][0] [x] dp x 0 s = int(n[i]) int(n[i]) s x = len(n) len(n) x n = input() input() n inf = 10 ** 9 10 ** 9 inf dp = [[inf] * 2 for _ in range(x+1)] [[inf] * 2 for _ in range(x+1)] dp
n = input()
x = len(n)
inf = 10 ** 9
#dp[i][flg]...i桁目まで、未満フラグ=1
dp = [[inf] * 2 for _ in range(x+1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(x):
    s = int(n[i])
    dp[i+1][0] = min(dp[i][0] + s, dp[i][1] + 10 - s)
    dp[i+1][1] = min(dp[i][0] + s + 1, dp[i][1] + 9 - s)

print(dp[x][0])
