N = input()
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1

for i in range(len(N)):
    n = int(N[i])
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))

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

for i in range(len(N)):
    n = int(N[i])
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))

print(dp[-1][0])
