n = input()
len_n = len(n)
dp = [[0,2] for i in range(len_n+1)]
for k,v in enumerate(n[::-1]):
    v = int(v)
    dp[k+1][0] = min(dp[k][0] + v, dp[k][1] + v)
    dp[k+1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1)
print(min(dp[len_n])) n = input() n input() input len_n = len(n) len_n len(n) len n [0,2] for i in range(len_n+1) for i in range(len_n+1) i range(len_n+1) range len_n+1 len_n 1 for i in range(len_n+1) [0,2] 0 2 dp = [[0,2] for i in range(len_n+1)] dp [[0,2] for i in range(len_n+1)] for k,v in enumerate(n[::-1]):
    v = int(v)
    dp[k+1][0] = min(dp[k][0] + v, dp[k][1] + v)
    dp[k+1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1) k v enumerate(n[::-1]) enumerate n[::-1] n ::-1 -1 v = int(v) v int(v) int v dp[k+1][0] = min(dp[k][0] + v, dp[k][1] + v) dp[k+1][0] [k+1] dp k+1 k 1 0 min(dp[k][0] + v, dp[k][1] + v) min dp[k][0] + v dp[k][0] [k] dp k 0 v dp[k][1] + v dp[k][1] [k] dp k 1 v dp[k+1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1) dp[k+1][1] [k+1] dp k+1 k 1 1 min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1) min dp[k][0] + 10 - v + 1 dp[k][0] + 10 - v dp[k][0] + 10 dp[k][0] [k] dp k 0 10 v 1 dp[k][1] + 10 - v - 1 dp[k][1] + 10 - v dp[k][1] + 10 dp[k][1] [k] dp k 1 10 v 1 print(min(dp[len_n])) print min(dp[len_n]) min dp[len_n] dp len_n len_n = len(n) len(n) len_n dp = [[0,2] for i in range(len_n+1)] [[0,2] for i in range(len_n+1)] dp n = input() input() n v = int(v) int(v) v
n = input()
len_n = len(n)
dp = [[0,2] for i in range(len_n+1)]
for k,v in enumerate(n[::-1]):
    v = int(v)
    dp[k+1][0] = min(dp[k][0] + v, dp[k][1] + v)
    dp[k+1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1)
print(min(dp[len_n]))
