N = list(map(int, list(input())))[::-1]
dp0 = [float('inf')] * (len(N)+1)
dp1 = [float('inf')] * (len(N)+1)
dp0[0] = 0
dp1[0] = 1
for i in range(len(N)):
    dp0[i+1] = min(dp0[i] + N[i], dp1[i] + 10 - N[i])
    dp1[i+1] = min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i])
print(dp0[-1]) N = list(map(int, list(input())))[::-1] N list(map(int, list(input())))[::-1] (map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input ::-1 -1 dp0 = [float('inf')] * (len(N)+1) dp0 [float('inf')] * (len(N)+1) [float('inf')] float('inf') float 'inf' len(N)+1 len(N) len N 1 dp1 = [float('inf')] * (len(N)+1) dp1 [float('inf')] * (len(N)+1) [float('inf')] float('inf') float 'inf' len(N)+1 len(N) len N 1 dp0[0] = 0 dp0[0] dp0 0 0 dp1[0] = 1 dp1[0] dp1 0 1 for i in range(len(N)):
    dp0[i+1] = min(dp0[i] + N[i], dp1[i] + 10 - N[i])
    dp1[i+1] = min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i]) i range(len(N)) range len(N) len N dp0[i+1] = min(dp0[i] + N[i], dp1[i] + 10 - N[i]) dp0[i+1] dp0 i+1 i 1 min(dp0[i] + N[i], dp1[i] + 10 - N[i]) min dp0[i] + N[i] dp0[i] dp0 i N[i] N i dp1[i] + 10 - N[i] dp1[i] + 10 dp1[i] dp1 i 10 N[i] N i dp1[i+1] = min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i]) dp1[i+1] dp1 i+1 i 1 min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i]) min dp0[i] + N[i] + 1 dp0[i] + N[i] dp0[i] dp0 i N[i] N i 1 dp1[i] + 9 - N[i] dp1[i] + 9 dp1[i] dp1 i 9 N[i] N i print(dp0[-1]) print dp0[-1] dp0 -1 N = list(map(int, list(input())))[::-1] list(map(int, list(input())))[::-1] N dp0 = [float('inf')] * (len(N)+1) [float('inf')] * (len(N)+1) dp0 dp1 = [float('inf')] * (len(N)+1) [float('inf')] * (len(N)+1) dp1
N = list(map(int, list(input())))[::-1]
dp0 = [float('inf')] * (len(N)+1)
dp1 = [float('inf')] * (len(N)+1)
dp0[0] = 0
dp1[0] = 1
for i in range(len(N)):
    dp0[i+1] = min(dp0[i] + N[i], dp1[i] + 10 - N[i])
    dp1[i+1] = min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i])
print(dp0[-1])
