n = input()
n = n[::-1]
n += '0'
cnt = 0
dp = [[float('inf')] * 2 for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(len(n)):
    a = int(n[i])
    dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + a + 1)
    dp[i + 1][1] = min(dp[i][0] + 10 - a, dp[i][1] + 10 - (a + 1))
print(min(dp[len(n)][0], dp[len(n)][1])) n = input() n input() input n = n[::-1] n n[::-1] n ::-1 -1 n += '0' n '0' cnt = 0 cnt 0 [float('inf')] * 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) [float('inf')] * 2 [float('inf')] float('inf') float 'inf' 2 dp = [[float('inf')] * 2 for _ in range(len(n) + 1)] dp [[float('inf')] * 2 for _ in range(len(n) + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(len(n)):
    a = int(n[i])
    dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + a + 1)
    dp[i + 1][1] = min(dp[i][0] + 10 - a, dp[i][1] + 10 - (a + 1)) i range(len(n)) range len(n) len n a = int(n[i]) a int(n[i]) int n[i] n i dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + a + 1) dp[i + 1][0] [i + 1] dp i + 1 i 1 0 min(dp[i][0] + a, dp[i][1] + a + 1) min dp[i][0] + a dp[i][0] [i] dp i 0 a dp[i][1] + a + 1 dp[i][1] + a dp[i][1] [i] dp i 1 a 1 dp[i + 1][1] = min(dp[i][0] + 10 - a, dp[i][1] + 10 - (a + 1)) dp[i + 1][1] [i + 1] dp i + 1 i 1 1 min(dp[i][0] + 10 - a, dp[i][1] + 10 - (a + 1)) min dp[i][0] + 10 - a dp[i][0] + 10 dp[i][0] [i] dp i 0 10 a dp[i][1] + 10 - (a + 1) dp[i][1] + 10 dp[i][1] [i] dp i 1 10 a + 1 a 1 print(min(dp[len(n)][0], dp[len(n)][1])) print min(dp[len(n)][0], dp[len(n)][1]) min dp[len(n)][0] [len(n)] dp len(n) len n 0 dp[len(n)][1] [len(n)] dp len(n) len n 1 cnt = 0 0 cnt a = int(n[i]) int(n[i]) a n = n[::-1] n[::-1] n n = input() input() n dp = [[float('inf')] * 2 for _ in range(len(n) + 1)] [[float('inf')] * 2 for _ in range(len(n) + 1)] dp n += '0' '0' n
n = input()
n = n[::-1]
n += '0'
cnt = 0
dp = [[float('inf')] * 2 for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(len(n)):
    a = int(n[i])
    dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + a + 1)
    dp[i + 1][1] = min(dp[i][0] + 10 - a, dp[i][1] + 10 - (a + 1))
print(min(dp[len(n)][0], dp[len(n)][1]))