S = input()
L = len(S)

dp = [[0 for _ in range(L + 1)] for over in range(2)]
dp[0][0] = 0
dp[1][0] = 1

for j, s in enumerate(S, 1):
    n = int(s)
    dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n))
    dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1))
    
print(dp[0][-1]) S = input() S input() input L = len(S) L len(S) len S [0 for _ in range(L + 1)] for over in range(2) for over in range(2) over range(2) range 2 for over in range(2) [0 for _ in range(L + 1)] dp = [[0 for _ in range(L + 1)] for over in range(2)] dp [[0 for _ in range(L + 1)] for over in range(2)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[1][0] = 1 dp[1][0] [1] dp 1 0 1 for j, s in enumerate(S, 1):
    n = int(s)
    dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n))
    dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1))
     j s enumerate(S, 1) enumerate S 1 n = int(s) n int(s) int s dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n)) dp[0][j] [0] dp 0 j min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n)) min dp[0][j - 1] + n dp[0][j - 1] [0] dp 0 j - 1 j 1 n dp[1][j - 1] + (10 - n) dp[1][j - 1] [1] dp 1 j - 1 j 1 10 - n 10 n dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1)) dp[1][j] [1] dp 1 j min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1)) min dp[0][j - 1] + (n + 1) dp[0][j - 1] [0] dp 0 j - 1 j 1 n + 1 n 1 dp[1][j - 1] + (10 - n - 1) dp[1][j - 1] [1] dp 1 j - 1 j 1 10 - n - 1 10 - n 10 n 1 print(dp[0][-1]) print dp[0][-1] [0] dp 0 -1 L = len(S) len(S) L n = int(s) int(s) n S = input() input() S dp = [[0 for _ in range(L + 1)] for over in range(2)] [[0 for _ in range(L + 1)] for over in range(2)] dp
S = input()
L = len(S)

dp = [[0 for _ in range(L + 1)] for over in range(2)]
dp[0][0] = 0
dp[1][0] = 1

for j, s in enumerate(S, 1):
    n = int(s)
    dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n))
    dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1))
    
print(dp[0][-1])