N = input()
INF = 10 ** 9
N = N[::-1] + '0'
dp = [[INF for _ in range(2)] for _ in range(len(N) + 1)]
dp[0][0] = 0

for i in range(len(N)):
    for j in range(2):
        x = int(N[i])
        x += j
        for k in range(10):
            nexti = i + 1
            nextj = 0
            b = k - x
            if b < 0:
                nextj = 1
                b += 10
            dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b)
print(dp[len(N)][0]) N = input() N input() input INF = 10 ** 9 INF 10 ** 9 10 9 N = N[::-1] + '0' N N[::-1] + '0' N[::-1] N ::-1 -1 '0' [INF for _ in range(2)] for _ in range(len(N) + 1) for _ in range(len(N) + 1) _ range(len(N) + 1) range len(N) + 1 len(N) len N 1 for _ in range(len(N) + 1) [INF for _ in range(2)] dp = [[INF for _ in range(2)] for _ in range(len(N) + 1)] dp [[INF for _ in range(2)] for _ in range(len(N) + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(len(N)):
    for j in range(2):
        x = int(N[i])
        x += j
        for k in range(10):
            nexti = i + 1
            nextj = 0
            b = k - x
            if b < 0:
                nextj = 1
                b += 10
            dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b) i range(len(N)) range len(N) len N for j in range(2):
        x = int(N[i])
        x += j
        for k in range(10):
            nexti = i + 1
            nextj = 0
            b = k - x
            if b < 0:
                nextj = 1
                b += 10
            dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b) j range(2) range 2 x = int(N[i]) x int(N[i]) int N[i] N i x += j x j for k in range(10):
            nexti = i + 1
            nextj = 0
            b = k - x
            if b < 0:
                nextj = 1
                b += 10
            dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b) k range(10) range 10 nexti = i + 1 nexti i + 1 i 1 nextj = 0 nextj 0 b = k - x b k - x k x if b < 0:
                nextj = 1
                b += 10
             b < 0 b 0 nextj = 1 nextj 1 b += 10 b 10 dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b) dp[nexti][nextj] [nexti] dp nexti nextj min(dp[nexti][nextj], dp[i][j] + k + b) min dp[nexti][nextj] [nexti] dp nexti nextj dp[i][j] + k + b dp[i][j] + k dp[i][j] [i] dp i j k b print(dp[len(N)][0]) print dp[len(N)][0] [len(N)] dp len(N) len N 0 dp = [[INF for _ in range(2)] for _ in range(len(N) + 1)] [[INF for _ in range(2)] for _ in range(len(N) + 1)] dp nextj = 1 1 nextj INF = 10 ** 9 10 ** 9 INF N = N[::-1] + '0' N[::-1] + '0' N nextj = 0 0 nextj nexti = i + 1 i + 1 nexti b += 10 10 b N = input() input() N x += j j x b = k - x k - x b x = int(N[i]) int(N[i]) x
N = input()
INF = 10 ** 9
N = N[::-1] + '0'
dp = [[INF for _ in range(2)] for _ in range(len(N) + 1)]
dp[0][0] = 0

for i in range(len(N)):
    for j in range(2):
        x = int(N[i])
        x += j
        for k in range(10):
            nexti = i + 1
            nextj = 0
            b = k - x
            if b < 0:
                nextj = 1
                b += 10
            dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b)
print(dp[len(N)][0])