INF = 10**18

S = '0'+input()
N = len(S)

dp = [[INF,INF] for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(2):
        s = int(S[-1-i])
        s += j

        for a in range(10):
            ni = i+1
            nj = 0
            b = a-s
            if b < 0:
                b += 10
                nj = 1
            dp[ni][nj] = min(dp[ni][nj],dp[i][j]+a+b)

print(dp[N][0]) INF = 10**18 INF 10**18 10 18 S = '0'+input() S '0'+input() '0' input() input N = len(S) N len(S) len S [INF,INF] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [INF,INF] INF INF dp = [[INF,INF] for _ in range(N+1)] dp [[INF,INF] for _ 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):
        s = int(S[-1-i])
        s += j

        for a in range(10):
            ni = i+1
            nj = 0
            b = a-s
            if b < 0:
                b += 10
                nj = 1
            dp[ni][nj] = min(dp[ni][nj],dp[i][j]+a+b) i range(N) range N for j in range(2):
        s = int(S[-1-i])
        s += j

        for a in range(10):
            ni = i+1
            nj = 0
            b = a-s
            if b < 0:
                b += 10
                nj = 1
            dp[ni][nj] = min(dp[ni][nj],dp[i][j]+a+b) j range(2) range 2 s = int(S[-1-i]) s int(S[-1-i]) int S[-1-i] S -1-i -1 i s += j s j for a in range(10):
            ni = i+1
            nj = 0
            b = a-s
            if b < 0:
                b += 10
                nj = 1
            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-s b a-s a s if b < 0:
                b += 10
                nj = 1
             b < 0 b 0 b += 10 b 10 nj = 1 nj 1 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 nj = 0 0 nj b = a-s a-s b dp = [[INF,INF] for _ in range(N+1)] [[INF,INF] for _ in range(N+1)] dp s = int(S[-1-i]) int(S[-1-i]) s S = '0'+input() '0'+input() S ni = i+1 i+1 ni b += 10 10 b nj = 1 1 nj INF = 10**18 10**18 INF N = len(S) len(S) N s += j j s
INF = 10**18

S = '0'+input()
N = len(S)

dp = [[INF,INF] for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(2):
        s = int(S[-1-i])
        s += j

        for a in range(10):
            ni = i+1
            nj = 0
            b = a-s
            if b < 0:
                b += 10
                nj = 1
            dp[ni][nj] = min(dp[ni][nj],dp[i][j]+a+b)

print(dp[N][0])