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