import sys
input = sys.stdin.readline

N = [int(x) for x in input().rstrip()][::-1] + [0]
L = len(N)

dp = [[10 ** 18, 10 ** 18] for _ in range(L)]
dp[0] = [N[0], 10 - N[0]]

for i in range(1, L):
    n = N[i]
    dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1))
    dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1)))

print(dp[-1][0]) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline int(x) for x in input().rstrip() for x in input().rstrip() x input().rstrip() ().rstrip () input rstrip for x in input().rstrip() int(x) int x N = [int(x) for x in input().rstrip()][::-1] + [0] N [int(x) for x in input().rstrip()][::-1] + [0] [int(x) for x in input().rstrip()][::-1] [int(x) for x in input().rstrip()] ::-1 -1 [0] 0 L = len(N) L len(N) len N [10 ** 18, 10 ** 18] for _ in range(L) for _ in range(L) _ range(L) range L for _ in range(L) [10 ** 18, 10 ** 18] 10 ** 18 10 18 10 ** 18 10 18 dp = [[10 ** 18, 10 ** 18] for _ in range(L)] dp [[10 ** 18, 10 ** 18] for _ in range(L)] dp[0] = [N[0], 10 - N[0]] dp[0] dp 0 [N[0], 10 - N[0]] N[0] N 0 10 - N[0] 10 N[0] N 0 for i in range(1, L):
    n = N[i]
    dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1))
    dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1))) i range(1, L) range 1 L n = N[i] n N[i] N i dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1)) dp[i][0] [i] dp i 0 min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1)) min dp[i - 1][0] + n dp[i - 1][0] [i - 1] dp i - 1 i 1 0 n dp[i - 1][1] + (n + 1) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 n + 1 n 1 dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1))) dp[i][1] [i] dp i 1 min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1))) min dp[i - 1][0] + (10 - n) dp[i - 1][0] [i - 1] dp i - 1 i 1 0 10 - n 10 n dp[i - 1][1] + (10 - (n + 1)) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - (n + 1) 10 n + 1 n 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 input = sys.stdin.readline sys.stdin.readline input dp = [[10 ** 18, 10 ** 18] for _ in range(L)] [[10 ** 18, 10 ** 18] for _ in range(L)] dp L = len(N) len(N) L N = [int(x) for x in input().rstrip()][::-1] + [0] [int(x) for x in input().rstrip()][::-1] + [0] N n = N[i] N[i] n
import sys
input = sys.stdin.readline

N = [int(x) for x in input().rstrip()][::-1] + [0]
L = len(N)

dp = [[10 ** 18, 10 ** 18] for _ in range(L)]
dp[0] = [N[0], 10 - N[0]]

for i in range(1, L):
    n = N[i]
    dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1))
    dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1)))

print(dp[-1][0])