import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# DP
INF = pow(10, 8)
L = len(N)
dp = [[INF] * 2 for _ in range(L+2)]
dp[0][0] = 0

# reverse
N = N[::-1] + '0'


for i in range(L+1):
    for j in range(2):
        # 繰り下がりも加味
        n = int(N[i])
        n += j

        # 更新後のdp
        ni = i + 1

        # 10通りから探索
        for a in range(10):
            nj = 0
            b = a - n

            # 繰り下がりあり
            if b < 0:
                b += 10
                nj = 1

            dp[ni][nj] = min(dp[i][j] + a + b, dp[ni][nj])

ans = min(dp[L+1][0], dp[L+1][1])

print(ans) import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 N = input() N input() input INF = pow(10, 8) INF pow(10, 8) pow 10 8 L = len(N) L len(N) len N [INF] * 2 for _ in range(L+2) for _ in range(L+2) _ range(L+2) range L+2 L 2 for _ in range(L+2) [INF] * 2 [INF] INF 2 dp = [[INF] * 2 for _ in range(L+2)] dp [[INF] * 2 for _ in range(L+2)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 N = N[::-1] + '0' N N[::-1] + '0' N[::-1] N ::-1 -1 '0' for i in range(L+1):
    for j in range(2):
        # 繰り下がりも加味
        n = int(N[i])
        n += j

        # 更新後のdp
        ni = i + 1

        # 10通りから探索
        for a in range(10):
            nj = 0
            b = a - n

            # 繰り下がりあり
            if b < 0:
                b += 10
                nj = 1

            dp[ni][nj] = min(dp[i][j] + a + b, dp[ni][nj]) i range(L+1) range L+1 L 1 for j in range(2):
        # 繰り下がりも加味
        n = int(N[i])
        n += j

        # 更新後のdp
        ni = i + 1

        # 10通りから探索
        for a in range(10):
            nj = 0
            b = a - n

            # 繰り下がりあり
            if b < 0:
                b += 10
                nj = 1

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

            # 繰り下がりあり
            if b < 0:
                b += 10
                nj = 1

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

             b < 0 b 0 b += 10 b 10 nj = 1 nj 1 dp[ni][nj] = min(dp[i][j] + a + b, dp[ni][nj]) dp[ni][nj] [ni] dp ni nj min(dp[i][j] + a + b, dp[ni][nj]) min dp[i][j] + a + b dp[i][j] + a dp[i][j] [i] dp i j a b dp[ni][nj] [ni] dp ni nj ans = min(dp[L+1][0], dp[L+1][1]) ans min(dp[L+1][0], dp[L+1][1]) min dp[L+1][0] [L+1] dp L+1 L 1 0 dp[L+1][1] [L+1] dp L+1 L 1 1 print(ans) print ans nj = 0 0 nj ni = i + 1 i + 1 ni b += 10 10 b n = int(N[i]) int(N[i]) n N = input() input() N ans = min(dp[L+1][0], dp[L+1][1]) min(dp[L+1][0], dp[L+1][1]) ans L = len(N) len(N) L N = N[::-1] + '0' N[::-1] + '0' N INF = pow(10, 8) pow(10, 8) INF b = a - n a - n b dp = [[INF] * 2 for _ in range(L+2)] [[INF] * 2 for _ in range(L+2)] dp n += j j n nj = 1 1 nj
import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# DP
INF = pow(10, 8)
L = len(N)
dp = [[INF] * 2 for _ in range(L+2)]
dp[0][0] = 0

# reverse
N = N[::-1] + '0'


for i in range(L+1):
    for j in range(2):
        # 繰り下がりも加味
        n = int(N[i])
        n += j

        # 更新後のdp
        ni = i + 1

        # 10通りから探索
        for a in range(10):
            nj = 0
            b = a - n

            # 繰り下がりあり
            if b < 0:
                b += 10
                nj = 1

            dp[ni][nj] = min(dp[i][j] + a + b, dp[ni][nj])

ans = min(dp[L+1][0], dp[L+1][1])

print(ans)
