S = input()
S = S[::-1]
S += '0'

n = len(S)
dp = [[10**20 for j in range(2)] for i in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(2):
        x = ord(S[i])-ord('0')
        x += j
        for a in range(10):
            ni = i+1
            nj = 0
            b = a-x
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b)

print(dp[n][0]) S = input() S input() input S = S[::-1] S S[::-1] S ::-1 -1 S += '0' S '0' n = len(S) n len(S) len S [10**20 for j in range(2)] for i in range(n + 1) for i in range(n + 1) i range(n + 1) range n + 1 n 1 for i in range(n + 1) [10**20 for j in range(2)] dp = [[10**20 for j in range(2)] for i in range(n + 1)] dp [[10**20 for j in range(2)] for i in range(n + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(n):
    for j in range(2):
        x = ord(S[i])-ord('0')
        x += j
        for a in range(10):
            ni = i+1
            nj = 0
            b = a-x
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b) i range(n) range n for j in range(2):
        x = ord(S[i])-ord('0')
        x += j
        for a in range(10):
            ni = i+1
            nj = 0
            b = a-x
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b) j range(2) range 2 x = ord(S[i])-ord('0') x ord(S[i])-ord('0') ord(S[i]) ord S[i] S i ord('0') ord '0' x += j x j for a in range(10):
            ni = i+1
            nj = 0
            b = a-x
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b) a range(10) range 10 ni = i+1 ni i+1 i 1 nj = 0 nj 0 b = a-x b a-x a x if b < 0:
                nj = 1
                b += 10
             b < 0 b 0 nj = 1 nj 1 b += 10 b 10 dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj], dp[i][j]+a+b) min dp[ni][nj] [ni] dp ni nj dp[i][j]+a+b dp[i][j]+a dp[i][j] [i] dp i j a b print(dp[n][0]) print dp[n][0] [n] dp n 0 S = S[::-1] S[::-1] S b = a-x a-x b nj = 1 1 nj x = ord(S[i])-ord('0') ord(S[i])-ord('0') x ni = i+1 i+1 ni S = input() input() S nj = 0 0 nj n = len(S) len(S) n S += '0' '0' S dp = [[10**20 for j in range(2)] for i in range(n + 1)] [[10**20 for j in range(2)] for i in range(n + 1)] dp b += 10 10 b x += j j x
S = input()
S = S[::-1]
S += '0'

n = len(S)
dp = [[10**20 for j in range(2)] for i in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for j in range(2):
        x = ord(S[i])-ord('0')
        x += j
        for a in range(10):
            ni = i+1
            nj = 0
            b = a-x
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j]+a+b)

print(dp[n][0])
