s = input()
n = len(s)

s = list(map(int, list(s)))
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
dp2[0] = 1

for k in range(n):
    dp1[k + 1] = min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]])
    dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1])

print(dp1[-1]) s = input() s input() input n = len(s) n len(s) len s s = list(map(int, list(s))) s list(map(int, list(s))) list map(int, list(s)) map int list(s) list s dp1 = [0] * (n + 1) dp1 [0] * (n + 1) [0] 0 n + 1 n 1 dp2 = [0] * (n + 1) dp2 [0] * (n + 1) [0] 0 n + 1 n 1 dp2[0] = 1 dp2[0] dp2 0 1 for k in range(n):
    dp1[k + 1] = min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]])
    dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1]) k range(n) range n dp1[k + 1] = min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]]) dp1[k + 1] dp1 k + 1 k 1 min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]]) min [dp1[k] + s[k]       , dp2[k] + 10 - s[k]] dp1[k] + s[k] dp1[k] dp1 k s[k] s k dp2[k] + 10 - s[k] dp2[k] + 10 dp2[k] dp2 k 10 s[k] s k dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1]) dp2[k + 1] dp2 k + 1 k 1 min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1]) min [dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1] dp1[k] + s[k] + 1 dp1[k] + s[k] dp1[k] dp1 k s[k] s k 1 dp2[k] + 10 - s[k] - 1 dp2[k] + 10 - s[k] dp2[k] + 10 dp2[k] dp2 k 10 s[k] s k 1 print(dp1[-1]) print dp1[-1] dp1 -1 s = input() input() s n = len(s) len(s) n dp2 = [0] * (n + 1) [0] * (n + 1) dp2 dp1 = [0] * (n + 1) [0] * (n + 1) dp1 s = list(map(int, list(s))) list(map(int, list(s))) s
s = input()
n = len(s)

s = list(map(int, list(s)))
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
dp2[0] = 1

for k in range(n):
    dp1[k + 1] = min([dp1[k] + s[k]       , dp2[k] + 10 - s[k]])
    dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1])

print(dp1[-1])