import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# TLE (Python3 3.4.3)
# AC  (PyPy3 (2.4.0))

# 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):
    n = int(N[i])

    # 更新後のdp
    ni = i + 1

    # 前の桁で繰り下がりなし
    # 同じだけ払う
    dp[ni][0] = min(dp[ni][0], dp[i][0] + n)

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

    # 前の桁で繰り下がりあり
    n += 1

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

    # 繰り下がりあり
    dp[ni][1] = min(dp[ni][1], dp[i][1] + (10 - n))


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):
    n = int(N[i])

    # 更新後のdp
    ni = i + 1

    # 前の桁で繰り下がりなし
    # 同じだけ払う
    dp[ni][0] = min(dp[ni][0], dp[i][0] + n)

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

    # 前の桁で繰り下がりあり
    n += 1

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

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

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

    # 繰り下がりあり
     n == 10 n 10 dp[ni][1] = min(dp[ni][1], dp[i][1]) dp[ni][1] [ni] dp ni 1 min(dp[ni][1], dp[i][1]) min dp[ni][1] [ni] dp ni 1 dp[i][1] [i] dp i 1 dp[ni][0] = min(dp[ni][0], dp[i][1] + n) dp[ni][0] [ni] dp ni 0 min(dp[ni][0], dp[i][1] + n) min dp[ni][0] [ni] dp ni 0 dp[i][1] + n dp[i][1] [i] dp i 1 n dp[ni][1] = min(dp[ni][1], dp[i][1] + (10 - n)) dp[ni][1] [ni] dp ni 1 min(dp[ni][1], dp[i][1] + (10 - n)) min dp[ni][1] [ni] dp ni 1 dp[i][1] + (10 - n) dp[i][1] [i] dp i 1 10 - n 10 n 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 n += 1 1 n N = input() input() N INF = pow(10, 8) pow(10, 8) INF 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 n = int(N[i]) int(N[i]) n
import sys
sys.setrecursionlimit(10 ** 7)

N = input()

# TLE (Python3 3.4.3)
# AC  (PyPy3 (2.4.0))

# 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):
    n = int(N[i])

    # 更新後のdp
    ni = i + 1

    # 前の桁で繰り下がりなし
    # 同じだけ払う
    dp[ni][0] = min(dp[ni][0], dp[i][0] + n)

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

    # 前の桁で繰り下がりあり
    n += 1

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

    # 繰り下がりあり
    dp[ni][1] = min(dp[ni][1], dp[i][1] + (10 - n))


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

print(ans)
