n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
        n_i = int(n[i])
        dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
        dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1])) n = input() n input() input n = n[::-1] + '0' n n[::-1] + '0' n[::-1] n ::-1 -1 '0' inf = 10 ** 9 inf 10 ** 9 10 9 [inf for _ in range(2)] for _ in range(len(n) + 1) for _ in range(len(n) + 1) _ range(len(n) + 1) range len(n) + 1 len(n) len n 1 for _ in range(len(n) + 1) [inf for _ in range(2)] dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)] dp [[inf for _ in range(2)] for _ in range(len(n) + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[0][1] = 2 dp[0][1] [0] dp 0 1 2 for i in range(len(n)):
        n_i = int(n[i])
        dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
        dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i) i range(len(n)) range len(n) len n n_i = int(n[i]) n_i int(n[i]) int n[i] n i dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i) dp[i + 1][0] [i + 1] dp i + 1 i 1 0 min(dp[i][0] + n_i, dp[i][1] + n_i) min dp[i][0] + n_i dp[i][0] [i] dp i 0 n_i dp[i][1] + n_i dp[i][1] [i] dp i 1 n_i dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i) dp[i + 1][1] [i + 1] dp i + 1 i 1 1 min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i) min dp[i][0] + 11 - n_i dp[i][0] + 11 dp[i][0] [i] dp i 0 11 n_i dp[i][1] + 9 - n_i dp[i][1] + 9 dp[i][1] [i] dp i 1 9 n_i print(min(dp[-1][0], dp[-1][1])) print min(dp[-1][0], dp[-1][1]) min dp[-1][0] [-1] dp -1 0 dp[-1][1] [-1] dp -1 1 n = input() input() n n = n[::-1] + '0' n[::-1] + '0' n dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)] [[inf for _ in range(2)] for _ in range(len(n) + 1)] dp inf = 10 ** 9 10 ** 9 inf n_i = int(n[i]) int(n[i]) n_i
n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
        n_i = int(n[i])
        dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
        dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1]))