N = input()
L = len(N)

# dp[i][j]: 紙幣の最小枚数
# i: 決定した桁数
# j: 1枚多めに渡すフラグ
dp = [[float("inf")] * 2 for _ in range(L + 1)]

# 初期条件
dp[0][0] = 0
dp[0][1] = 1

# 貰うDP
for i in range(L):
    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[L][0]) N = input() N input() input L = len(N) L len(N) len N [float("inf")] * 2 for _ in range(L + 1) for _ in range(L + 1) _ range(L + 1) range L + 1 L 1 for _ in range(L + 1) [float("inf")] * 2 [float("inf")] float("inf") float "inf" 2 dp = [[float("inf")] * 2 for _ in range(L + 1)] dp [[float("inf")] * 2 for _ in range(L + 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(L):
    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(L) range L 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] [i] dp i 1 10 - n 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] [i] dp i 0 n + 1 n 1 dp[i][1] + (10 - (n + 1)) dp[i][1] [i] dp i 1 10 - (n + 1) 10 n + 1 n 1 print(dp[L][0]) print dp[L][0] [L] dp L 0 N = input() input() N L = len(N) len(N) L dp = [[float("inf")] * 2 for _ in range(L + 1)] [[float("inf")] * 2 for _ in range(L + 1)] dp n = int(N[i]) int(N[i]) n
N = input()
L = len(N)

# dp[i][j]: 紙幣の最小枚数
# i: 決定した桁数
# j: 1枚多めに渡すフラグ
dp = [[float("inf")] * 2 for _ in range(L + 1)]

# 初期条件
dp[0][0] = 0
dp[0][1] = 1

# 貰うDP
for i in range(L):
    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[L][0])