n = input()
l = list(map(int, str(n)))

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

for i, v in enumerate(l):
    dp[i+1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v))
    dp[i+1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1)

print(dp[-1][0]) n = input() n input() input l = list(map(int, str(n))) l list(map(int, str(n))) list map(int, str(n)) map int str(n) str n [0] * 2 for _ in range(len(l)+1) for _ in range(len(l)+1) _ range(len(l)+1) range len(l)+1 len(l) len l 1 for _ in range(len(l)+1) [0] * 2 [0] 0 2 dp = [[0] * 2 for _ in range(len(l)+1)] dp [[0] * 2 for _ in range(len(l)+1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i, v in enumerate(l):
    dp[i+1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v))
    dp[i+1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1) i v enumerate(l) enumerate l dp[i+1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v)) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + v, dp[i][1] + (10 - v)) min dp[i][0] + v dp[i][0] [i] dp i 0 v dp[i][1] + (10 - v) dp[i][1] [i] dp i 1 10 - v 10 v dp[i+1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1) min dp[i][0] + v + 1 dp[i][0] + v dp[i][0] [i] dp i 0 v 1 dp[i][1] + (10 - v) - 1 dp[i][1] + (10 - v) dp[i][1] [i] dp i 1 10 - v 10 v 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 n = input() input() n l = list(map(int, str(n))) list(map(int, str(n))) l dp = [[0] * 2 for _ in range(len(l)+1)] [[0] * 2 for _ in range(len(l)+1)] dp
n = input()
l = list(map(int, str(n)))

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

for i, v in enumerate(l):
    dp[i+1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v))
    dp[i+1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1)

print(dp[-1][0])