N = list(input())
Y = [0]
for i in N:
    Y.append(int(i))
dp = [0 for i in range(len(N)+1)]
dp2 = [10 for i in range(len(N)+1)]
for i in range(1,len(N)+1):
    dp[i] = min(dp[i-1] + Y[i],dp2[i-1] + Y[i])
    dp2[i] = min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i])
    #print(str(dp[i])+":" +str(dp2[i]))
ans = min(dp[-1],dp2[-1])
#print(dp,dp2)
print(ans) N = list(input()) N list(input()) list input() input Y = [0] Y [0] 0 for i in N:
    Y.append(int(i)) i N Y.append(int(i)) Y.append Y append int(i) int i 0 for i in range(len(N)+1) for i in range(len(N)+1) i range(len(N)+1) range len(N)+1 len(N) len N 1 for i in range(len(N)+1) 0 dp = [0 for i in range(len(N)+1)] dp [0 for i in range(len(N)+1)] 10 for i in range(len(N)+1) for i in range(len(N)+1) i range(len(N)+1) range len(N)+1 len(N) len N 1 for i in range(len(N)+1) 10 dp2 = [10 for i in range(len(N)+1)] dp2 [10 for i in range(len(N)+1)] for i in range(1,len(N)+1):
    dp[i] = min(dp[i-1] + Y[i],dp2[i-1] + Y[i])
    dp2[i] = min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i])
    #print(str(dp[i])+":" +str(dp2[i])) i range(1,len(N)+1) range 1 len(N)+1 len(N) len N 1 dp[i] = min(dp[i-1] + Y[i],dp2[i-1] + Y[i]) dp[i] dp i min(dp[i-1] + Y[i],dp2[i-1] + Y[i]) min dp[i-1] + Y[i] dp[i-1] dp i-1 i 1 Y[i] Y i dp2[i-1] + Y[i] dp2[i-1] dp2 i-1 i 1 Y[i] Y i dp2[i] = min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i]) dp2[i] dp2 i min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i]) min dp[i-1] + 1 + 10 - Y[i] dp[i-1] + 1 + 10 dp[i-1] + 1 dp[i-1] dp i-1 i 1 1 10 Y[i] Y i dp2[i-1] - 1 + 10 - Y[i] dp2[i-1] - 1 + 10 dp2[i-1] - 1 dp2[i-1] dp2 i-1 i 1 1 10 Y[i] Y i ans = min(dp[-1],dp2[-1]) ans min(dp[-1],dp2[-1]) min dp[-1] dp -1 dp2[-1] dp2 -1 print(ans) print ans N = list(input()) list(input()) N dp = [0 for i in range(len(N)+1)] [0 for i in range(len(N)+1)] dp ans = min(dp[-1],dp2[-1]) min(dp[-1],dp2[-1]) ans Y = [0] [0] Y dp2 = [10 for i in range(len(N)+1)] [10 for i in range(len(N)+1)] dp2
N = list(input())
Y = [0]
for i in N:
    Y.append(int(i))
dp = [0 for i in range(len(N)+1)]
dp2 = [10 for i in range(len(N)+1)]
for i in range(1,len(N)+1):
    dp[i] = min(dp[i-1] + Y[i],dp2[i-1] + Y[i])
    dp2[i] = min(dp[i-1] + 1 + 10 - Y[i],dp2[i-1] - 1 + 10 - Y[i])
    #print(str(dp[i])+":" +str(dp2[i]))
ans = min(dp[-1],dp2[-1])
#print(dp,dp2)
print(ans)