s = input()
keta = len(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1)) s = input() s input() input keta = len(s) keta len(s) len s infi = 10 ** 10 infi 10 ** 10 10 10 [infi] * (2) for _ in range(keta + 1) for _ in range(keta + 1) _ range(keta + 1) range keta + 1 keta 1 for _ in range(keta + 1) [infi] * (2) [infi] infi 2 dp = [[infi] * (2) for _ in range(keta + 1)] dp [[infi] * (2) for _ in range(keta + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 s = s[::-1] s s[::-1] s ::-1 -1 for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) i range(keta) range keta for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) j range(2) range 2 num = int(s[i]) num int(s[i]) int s[i] s i if j == 1:
            num += 1
         j == 1 j 1 num += 1 num 1 ni = i + 1 ni i + 1 i 1 if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) num == 10 num 10 nj = 1 nj 1 num = 0 num 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j] + num) min dp[ni][nj] [ni] dp ni nj dp[i][j] + num dp[i][j] [i] dp i j num elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
         num < 5 num 5 nj = 0 nj 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j] + num) min dp[ni][nj] [ni] dp ni nj dp[i][j] + num dp[i][j] [i] dp i j num elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
         num > 5 num 5 nj = 1 nj 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j] + 10 - num) min dp[ni][nj] [ni] dp ni nj dp[i][j] + 10 - num dp[i][j] + 10 dp[i][j] [i] dp i j 10 num nj = 0 nj 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j] + num) min dp[ni][nj] [ni] dp ni nj dp[i][j] + num dp[i][j] [i] dp i j num nj = 1 nj 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j] + 10 - num) min dp[ni][nj] [ni] dp ni nj dp[i][j] + 10 - num dp[i][j] + 10 dp[i][j] [i] dp i j 10 num print(min(dp[keta][0], dp[keta][1] + 1)) print min(dp[keta][0], dp[keta][1] + 1) min dp[keta][0] [keta] dp keta 0 dp[keta][1] + 1 dp[keta][1] [keta] dp keta 1 1 dp = [[infi] * (2) for _ in range(keta + 1)] [[infi] * (2) for _ in range(keta + 1)] dp s = input() input() s nj = 1 1 nj num = 0 0 num nj = 0 0 nj nj = 1 1 nj nj = 1 1 nj keta = len(s) len(s) keta num = int(s[i]) int(s[i]) num ni = i + 1 i + 1 ni nj = 0 0 nj infi = 10 ** 10 10 ** 10 infi num += 1 1 num s = s[::-1] s[::-1] s
s = input()
keta = len(s)
infi = 10 ** 10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))
