n = input()

dp1 = [0] * (len(n) + 1)
dp2 = [1] * (len(n) + 1)

for i in range(1, len(n) + 1):
    m = int(n[i - 1])
    dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m)
    dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m)

print(dp1[-1]) n = input() n input() input dp1 = [0] * (len(n) + 1) dp1 [0] * (len(n) + 1) [0] 0 len(n) + 1 len(n) len n 1 dp2 = [1] * (len(n) + 1) dp2 [1] * (len(n) + 1) [1] 1 len(n) + 1 len(n) len n 1 for i in range(1, len(n) + 1):
    m = int(n[i - 1])
    dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m)
    dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m) i range(1, len(n) + 1) range 1 len(n) + 1 len(n) len n 1 m = int(n[i - 1]) m int(n[i - 1]) int n[i - 1] n i - 1 i 1 dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m) dp1[i] dp1 i min(dp1[i - 1] + m, dp2[i - 1] + 10 - m) min dp1[i - 1] + m dp1[i - 1] dp1 i - 1 i 1 m dp2[i - 1] + 10 - m dp2[i - 1] + 10 dp2[i - 1] dp2 i - 1 i 1 10 m dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m) dp2[i] dp2 i min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m) min dp1[i - 1] + m + 1 dp1[i - 1] + m dp1[i - 1] dp1 i - 1 i 1 m 1 dp2[i - 1] + 9 - m dp2[i - 1] + 9 dp2[i - 1] dp2 i - 1 i 1 9 m print(dp1[-1]) print dp1[-1] dp1 -1 dp1 = [0] * (len(n) + 1) [0] * (len(n) + 1) dp1 m = int(n[i - 1]) int(n[i - 1]) m n = input() input() n dp2 = [1] * (len(n) + 1) [1] * (len(n) + 1) dp2
n = input()

dp1 = [0] * (len(n) + 1)
dp2 = [1] * (len(n) + 1)

for i in range(1, len(n) + 1):
    m = int(n[i - 1])
    dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m)
    dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m)

print(dp1[-1])