string_n = input()
string_n = '0' + string_n
len_n = len(string_n)
dp = [[0, 0] for _ in range(len_n + 1)]

for i in range(len_n):
    for j in range(2):
        if j == 0:
            dp[i + 1][j] = min(dp[i]) + int(string_n[i])
        else:
            dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i]))

print(min(dp[len_n])) string_n = input() string_n input() input string_n = '0' + string_n string_n '0' + string_n '0' string_n len_n = len(string_n) len_n len(string_n) len string_n [0, 0] 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, 0] 0 0 dp = [[0, 0] for _ in range(len_n + 1)] dp [[0, 0] for _ in range(len_n + 1)] for i in range(len_n):
    for j in range(2):
        if j == 0:
            dp[i + 1][j] = min(dp[i]) + int(string_n[i])
        else:
            dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i])) i range(len_n) range len_n for j in range(2):
        if j == 0:
            dp[i + 1][j] = min(dp[i]) + int(string_n[i])
        else:
            dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i])) j range(2) range 2 if j == 0:
            dp[i + 1][j] = min(dp[i]) + int(string_n[i])
        else:
            dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i])) j == 0 j 0 dp[i + 1][j] = min(dp[i]) + int(string_n[i]) dp[i + 1][j] [i + 1] dp i + 1 i 1 j min(dp[i]) + int(string_n[i]) min(dp[i]) min dp[i] dp i int(string_n[i]) int string_n[i] string_n i dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i])) dp[i + 1][j] [i + 1] dp i + 1 i 1 j min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i])) min dp[i][0] + 1 + 10 - int(string_n[i]) dp[i][0] + 1 + 10 dp[i][0] + 1 dp[i][0] [i] dp i 0 1 10 int(string_n[i]) int string_n[i] string_n i dp[i][1] + 9 - int(string_n[i]) dp[i][1] + 9 dp[i][1] [i] dp i 1 9 int(string_n[i]) int string_n[i] string_n i print(min(dp[len_n])) print min(dp[len_n]) min dp[len_n] dp len_n string_n = input() input() string_n dp = [[0, 0] for _ in range(len_n + 1)] [[0, 0] for _ in range(len_n + 1)] dp string_n = '0' + string_n '0' + string_n string_n len_n = len(string_n) len(string_n) len_n
string_n = input()
string_n = '0' + string_n
len_n = len(string_n)
dp = [[0, 0] for _ in range(len_n + 1)]

for i in range(len_n):
    for j in range(2):
        if j == 0:
            dp[i + 1][j] = min(dp[i]) + int(string_n[i])
        else:
            dp[i + 1][j] = min(dp[i][0] + 1 + 10 - int(string_n[i]), dp[i][1] + 9 - int(string_n[i]))

print(min(dp[len_n]))