n = input()
len_n = len(n)

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

for i in range(1, len_n+1):
    tmp = int(n[i-1])

    dp[i][0] = min(dp[i-1][0] + tmp, dp[i-1][1] + (10-tmp))
    dp[i][1] = dp[i-1][1] + (9-tmp)
    if(tmp != 9):
        dp[i][1] = min(dp[i][1], dp[i-1][0] + tmp + 1)

ans = min(dp[-1][0], dp[-1][1] + 1)

print(ans) n = input() n input() input len_n = len(n) len_n len(n) len n [0]*2 for _ in range(len_n + 1) for _ in range(len_n + 1) _ range(len_n + 1) range len_n + 1 len_n 1 for _ in range(len_n + 1) [0]*2 [0] 0 2 dp = [[0]*2 for _ in range(len_n + 1)] dp [[0]*2 for _ in range(len_n + 1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(1, len_n+1):
    tmp = int(n[i-1])

    dp[i][0] = min(dp[i-1][0] + tmp, dp[i-1][1] + (10-tmp))
    dp[i][1] = dp[i-1][1] + (9-tmp)
    if(tmp != 9):
        dp[i][1] = min(dp[i][1], dp[i-1][0] + tmp + 1) i range(1, len_n+1) range 1 len_n+1 len_n 1 tmp = int(n[i-1]) tmp int(n[i-1]) int n[i-1] n i-1 i 1 dp[i][0] = min(dp[i-1][0] + tmp, dp[i-1][1] + (10-tmp)) dp[i][0] [i] dp i 0 min(dp[i-1][0] + tmp, dp[i-1][1] + (10-tmp)) min dp[i-1][0] + tmp dp[i-1][0] [i-1] dp i-1 i 1 0 tmp dp[i-1][1] + (10-tmp) dp[i-1][1] [i-1] dp i-1 i 1 1 10-tmp 10 tmp dp[i][1] = dp[i-1][1] + (9-tmp) dp[i][1] [i] dp i 1 dp[i-1][1] + (9-tmp) dp[i-1][1] [i-1] dp i-1 i 1 1 9-tmp 9 tmp if(tmp != 9):
        dp[i][1] = min(dp[i][1], dp[i-1][0] + tmp + 1) tmp != 9 tmp 9 dp[i][1] = min(dp[i][1], dp[i-1][0] + tmp + 1) dp[i][1] [i] dp i 1 min(dp[i][1], dp[i-1][0] + tmp + 1) min dp[i][1] [i] dp i 1 dp[i-1][0] + tmp + 1 dp[i-1][0] + tmp dp[i-1][0] [i-1] dp i-1 i 1 0 tmp 1 ans = min(dp[-1][0], dp[-1][1] + 1) ans min(dp[-1][0], dp[-1][1] + 1) min dp[-1][0] [-1] dp -1 0 dp[-1][1] + 1 dp[-1][1] [-1] dp -1 1 1 print(ans) print ans dp = [[0]*2 for _ in range(len_n + 1)] [[0]*2 for _ in range(len_n + 1)] dp n = input() input() n tmp = int(n[i-1]) int(n[i-1]) tmp ans = min(dp[-1][0], dp[-1][1] + 1) min(dp[-1][0], dp[-1][1] + 1) ans len_n = len(n) len(n) len_n
n = input()
len_n = len(n)

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

for i in range(1, len_n+1):
    tmp = int(n[i-1])

    dp[i][0] = min(dp[i-1][0] + tmp, dp[i-1][1] + (10-tmp))
    dp[i][1] = dp[i-1][1] + (9-tmp)
    if(tmp != 9):
        dp[i][1] = min(dp[i][1], dp[i-1][0] + tmp + 1)

ans = min(dp[-1][0], dp[-1][1] + 1)

print(ans)