N = input()[::-1]
l = len(N)
dp = [[0,0] for i in range(l+1)]

for i in range(l):
    dp[i+1][0] = min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1)
    if i == 0:
        dp[i+1][1] = 10 - int(N[i])
    else:
        dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i]))

print(min(dp[-1][0],dp[-1][1]+1)) N = input()[::-1] N input()[::-1] () input ::-1 -1 l = len(N) l len(N) len N [0,0] for i in range(l+1) for i in range(l+1) i range(l+1) range l+1 l 1 for i in range(l+1) [0,0] 0 0 dp = [[0,0] for i in range(l+1)] dp [[0,0] for i in range(l+1)] for i in range(l):
    dp[i+1][0] = min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1)
    if i == 0:
        dp[i+1][1] = 10 - int(N[i])
    else:
        dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i])) i range(l) range l dp[i+1][0] = min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1) min dp[i][0] + int(N[i]) dp[i][0] [i] dp i 0 int(N[i]) int N[i] N i dp[i][1] + int(N[i]) + 1 dp[i][1] + int(N[i]) dp[i][1] [i] dp i 1 int(N[i]) int N[i] N i 1 if i == 0:
        dp[i+1][1] = 10 - int(N[i])
    else:
        dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i])) i == 0 i 0 dp[i+1][1] = 10 - int(N[i]) dp[i+1][1] [i+1] dp i+1 i 1 1 10 - int(N[i]) 10 int(N[i]) int N[i] N i dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i])) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i])) min dp[i][0] + 10 - int(N[i]) dp[i][0] + 10 dp[i][0] [i] dp i 0 10 int(N[i]) int N[i] N i dp[i][1] + 9 - int(N[i]) dp[i][1] + 9 dp[i][1] [i] dp i 1 9 int(N[i]) int N[i] N i print(min(dp[-1][0],dp[-1][1]+1)) print min(dp[-1][0],dp[-1][1]+1) min dp[-1][0] [-1] dp -1 0 dp[-1][1]+1 dp[-1][1] [-1] dp -1 1 1 N = input()[::-1] input()[::-1] N l = len(N) len(N) l dp = [[0,0] for i in range(l+1)] [[0,0] for i in range(l+1)] dp
N = input()[::-1]
l = len(N)
dp = [[0,0] for i in range(l+1)]

for i in range(l):
    dp[i+1][0] = min(dp[i][0] + int(N[i]), dp[i][1] + int(N[i]) + 1)
    if i == 0:
        dp[i+1][1] = 10 - int(N[i])
    else:
        dp[i+1][1] = min(dp[i][0] + 10 - int(N[i]), dp[i][1] + 9 - int(N[i]))

print(min(dp[-1][0],dp[-1][1]+1))
