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

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

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

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

print(dp[len(N)][0])
