import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# TLE ()

# 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

        # 同じだけ払う
        if n < 10:
            dp[ni][0] = min(dp[ni][0], dp[i][j] + n)
        else:
            dp[ni][1] = min(dp[ni][1], dp[i][j])

        # 繰り下がりあり
        if n > 0:
            dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n))
        else:
            dp[ni][0] = min(dp[ni][0], dp[i][j])


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

        # 同じだけ払う
        if n < 10:
            dp[ni][0] = min(dp[ni][0], dp[i][j] + n)
        else:
            dp[ni][1] = min(dp[ni][1], dp[i][j])

        # 繰り下がりあり
        if n > 0:
            dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n))
        else:
            dp[ni][0] = min(dp[ni][0], dp[i][j]) i range(L+1) range L+1 L 1 for j in range(2):
        # 繰り下がりも加味
        n = int(N[i])
        n += j

        # 更新後のdp
        ni = i + 1

        # 同じだけ払う
        if n < 10:
            dp[ni][0] = min(dp[ni][0], dp[i][j] + n)
        else:
            dp[ni][1] = min(dp[ni][1], dp[i][j])

        # 繰り下がりあり
        if n > 0:
            dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n))
        else:
            dp[ni][0] = min(dp[ni][0], dp[i][j]) 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 if n < 10:
            dp[ni][0] = min(dp[ni][0], dp[i][j] + n)
        else:
            dp[ni][1] = min(dp[ni][1], dp[i][j])

        # 繰り下がりあり
         n < 10 n 10 dp[ni][0] = min(dp[ni][0], dp[i][j] + n) dp[ni][0] [ni] dp ni 0 min(dp[ni][0], dp[i][j] + n) min dp[ni][0] [ni] dp ni 0 dp[i][j] + n dp[i][j] [i] dp i j n dp[ni][1] = min(dp[ni][1], dp[i][j]) dp[ni][1] [ni] dp ni 1 min(dp[ni][1], dp[i][j]) min dp[ni][1] [ni] dp ni 1 dp[i][j] [i] dp i j if n > 0:
            dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n))
        else:
            dp[ni][0] = min(dp[ni][0], dp[i][j]) n > 0 n 0 dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n)) dp[ni][1] [ni] dp ni 1 min(dp[ni][1], dp[i][j] + (10 - n)) min dp[ni][1] [ni] dp ni 1 dp[i][j] + (10 - n) dp[i][j] [i] dp i j 10 - n 10 n dp[ni][0] = min(dp[ni][0], dp[i][j]) dp[ni][0] [ni] dp ni 0 min(dp[ni][0], dp[i][j]) min dp[ni][0] [ni] dp ni 0 dp[i][j] [i] dp i j 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 dp = [[INF] * 2 for _ in range(L+2)] [[INF] * 2 for _ in range(L+2)] dp N = N[::-1] + '0' N[::-1] + '0' N L = len(N) len(N) L ni = i + 1 i + 1 ni ans = min(dp[L+1][0], dp[L+1][1]) min(dp[L+1][0], dp[L+1][1]) ans INF = pow(10, 8) pow(10, 8) INF n = int(N[i]) int(N[i]) n n += j j n N = input() input() N
import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# TLE ()

# 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

        # 同じだけ払う
        if n < 10:
            dp[ni][0] = min(dp[ni][0], dp[i][j] + n)
        else:
            dp[ni][1] = min(dp[ni][1], dp[i][j])

        # 繰り下がりあり
        if n > 0:
            dp[ni][1] = min(dp[ni][1], dp[i][j] + (10 - n))
        else:
            dp[ni][0] = min(dp[ni][0], dp[i][j])


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

print(ans)
