N = input()

N = [int(i) for i in list(N)][::-1] + [0]
# print(N)
lenN = len(N)

dp = [[float('inf')]*2 for _ in range(len(N)+1)]
dp[0][0] = 0


for i in range(1, len(N)+1):
    res0 = float('inf')
    res1 = float('inf')

    for j in range(2):
        for num in range(10):
            if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
            if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
    dp[i][0] = res0
    dp[i][1] = res1

# print(dp)

print(min(dp[lenN][0], dp[lenN][1])) N = input() N input() input int(i) for i in list(N) for i in list(N) i list(N) list N for i in list(N) int(i) int i N = [int(i) for i in list(N)][::-1] + [0] N [int(i) for i in list(N)][::-1] + [0] [int(i) for i in list(N)][::-1] [int(i) for i in list(N)] ::-1 -1 [0] 0 lenN = len(N) lenN len(N) len N [float('inf')]*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) [float('inf')]*2 [float('inf')] float('inf') float 'inf' 2 dp = [[float('inf')]*2 for _ in range(len(N)+1)] dp [[float('inf')]*2 for _ in range(len(N)+1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(1, len(N)+1):
    res0 = float('inf')
    res1 = float('inf')

    for j in range(2):
        for num in range(10):
            if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
            if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
    dp[i][0] = res0
    dp[i][1] = res1

# print(dp) i range(1, len(N)+1) range 1 len(N)+1 len(N) len N 1 res0 = float('inf') res0 float('inf') float 'inf' res1 = float('inf') res1 float('inf') float 'inf' for j in range(2):
        for num in range(10):
            if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
            if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
     j range(2) range 2 for num in range(10):
            if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
            if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
     num range(10) range 10 if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
             j == 1 j 1 num = num - 1 num num - 1 num 1 if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
     num >= N[i-1] num N[i-1] N i-1 i 1 b = num - N[i-1] b num - N[i-1] num N[i-1] N i-1 i 1 res0 = min(res0, dp[i - 1][j] + num + b + j) res0 min(res0, dp[i - 1][j] + num + b + j) min res0 dp[i - 1][j] + num + b + j dp[i - 1][j] + num + b dp[i - 1][j] + num dp[i - 1][j] [i - 1] dp i - 1 i 1 j num b j b = num - N[i-1] + 10 b num - N[i-1] + 10 num - N[i-1] num N[i-1] N i-1 i 1 10 res1 = min(res1, dp[i - 1][j] + num + b + j) res1 min(res1, dp[i - 1][j] + num + b + j) min res1 dp[i - 1][j] + num + b + j dp[i - 1][j] + num + b dp[i - 1][j] + num dp[i - 1][j] [i - 1] dp i - 1 i 1 j num b j dp[i][0] = res0 dp[i][0] [i] dp i 0 res0 dp[i][1] = res1 dp[i][1] [i] dp i 1 res1 print(min(dp[lenN][0], dp[lenN][1])) print min(dp[lenN][0], dp[lenN][1]) min dp[lenN][0] [lenN] dp lenN 0 dp[lenN][1] [lenN] dp lenN 1 res1 = float('inf') float('inf') res1 res1 = min(res1, dp[i - 1][j] + num + b + j) min(res1, dp[i - 1][j] + num + b + j) res1 b = num - N[i-1] num - N[i-1] b res0 = min(res0, dp[i - 1][j] + num + b + j) min(res0, dp[i - 1][j] + num + b + j) res0 N = input() input() N N = [int(i) for i in list(N)][::-1] + [0] [int(i) for i in list(N)][::-1] + [0] N num = num - 1 num - 1 num dp = [[float('inf')]*2 for _ in range(len(N)+1)] [[float('inf')]*2 for _ in range(len(N)+1)] dp res0 = float('inf') float('inf') res0 b = num - N[i-1] + 10 num - N[i-1] + 10 b lenN = len(N) len(N) lenN
N = input()

N = [int(i) for i in list(N)][::-1] + [0]
# print(N)
lenN = len(N)

dp = [[float('inf')]*2 for _ in range(len(N)+1)]
dp[0][0] = 0


for i in range(1, len(N)+1):
    res0 = float('inf')
    res1 = float('inf')

    for j in range(2):
        for num in range(10):
            if j == 1:
                # 前に繰り下がりしていたらnumを減らす
                num = num - 1
            if num >= N[i-1]:
                b = num - N[i-1]
                res0 = min(res0, dp[i - 1][j] + num + b + j)
            else:
                b = num - N[i-1] + 10
                res1 = min(res1, dp[i - 1][j] + num + b + j)
    dp[i][0] = res0
    dp[i][1] = res1

# print(dp)

print(min(dp[lenN][0], dp[lenN][1]))
