S = input()
L = len(S)

INF = float('inf')
dp = [[INF]*(L+1) for _ in range(2)]
dp[0][0] = 0

for i in range(L):
    d = int(S[-1-i])
    dp[0][i+1] = min(dp[0][i], dp[1][i]) + d
    dp[1][i+1] = min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1)

print(min(dp[0][-1], dp[1][-1])) S = input() S input() input L = len(S) L len(S) len S INF = float('inf') INF float('inf') float 'inf' [INF]*(L+1) for _ in range(2) for _ in range(2) _ range(2) range 2 for _ in range(2) [INF]*(L+1) [INF] INF L+1 L 1 dp = [[INF]*(L+1) for _ in range(2)] dp [[INF]*(L+1) for _ in range(2)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(L):
    d = int(S[-1-i])
    dp[0][i+1] = min(dp[0][i], dp[1][i]) + d
    dp[1][i+1] = min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1) i range(L) range L d = int(S[-1-i]) d int(S[-1-i]) int S[-1-i] S -1-i -1 i dp[0][i+1] = min(dp[0][i], dp[1][i]) + d dp[0][i+1] [0] dp 0 i+1 i 1 min(dp[0][i], dp[1][i]) + d min(dp[0][i], dp[1][i]) min dp[0][i] [0] dp 0 i dp[1][i] [1] dp 1 i d dp[1][i+1] = min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1) dp[1][i+1] [1] dp 1 i+1 i 1 min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1) min dp[0][i] + 10-d+1 dp[0][i] + 10-d dp[0][i] + 10 dp[0][i] [0] dp 0 i 10 d 1 dp[1][i] + 10-d-1 dp[1][i] + 10-d dp[1][i] + 10 dp[1][i] [1] dp 1 i 10 d 1 print(min(dp[0][-1], dp[1][-1])) print min(dp[0][-1], dp[1][-1]) min dp[0][-1] [0] dp 0 -1 dp[1][-1] [1] dp 1 -1 INF = float('inf') float('inf') INF L = len(S) len(S) L d = int(S[-1-i]) int(S[-1-i]) d S = input() input() S dp = [[INF]*(L+1) for _ in range(2)] [[INF]*(L+1) for _ in range(2)] dp
S = input()
L = len(S)

INF = float('inf')
dp = [[INF]*(L+1) for _ in range(2)]
dp[0][0] = 0

for i in range(L):
    d = int(S[-1-i])
    dp[0][i+1] = min(dp[0][i], dp[1][i]) + d
    dp[1][i+1] = min(dp[0][i] + 10-d+1, dp[1][i] + 10-d-1)

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