def e_payment():

    N = eval(input())



    dp = (0, 1)  # 「最下位桁の 1 つ下の桁」を仮想した場合分けを省くための初期化

    for k, ch in enumerate(N[::-1]):  # 最下位の桁から見る

        n = int(ch)

        t1, t2 = dp

        a = min(t1 + n, t2 + n + 1)

        b = min(t1 + (10 - n), t2 + (10 - (n + 1)))

        dp = (a, b)

    return min(dp[0], dp[1] + 1)



print((e_payment()))
def e_payment_greedy():

    N = [int(i) for i in list(eval(input()))]

    s = N[::-1] + [0]  # [-d-1]: 最終的に 10**d 円札を何枚受渡ししたかが格納される



    for i in range(len(N)):

        if s[i] >= 6 or (s[i] == 5 and s[i + 1] >= 5):

            s[i] = 10 - s[i]

            s[i + 1] += 1

    return sum(s)



print((e_payment_greedy()))
def e_payment():
    N = eval(input())
    dp = (0, 1)  # 「最下位桁の 1 つ下の桁」を仮想した場合分けを省くための初期化
    for k, ch in enumerate(N[::-1]):  # 最下位の桁から見る
        n = int(ch)
        t1, t2 = dp
        a = min(t1 + n, t2 + n + 1)
        b = min(t1 + (10 - n), t2 + (10 - (n + 1)))
        dp = (a, b)
    return min(dp[0], dp[1] + 1)


print((e_payment()))

def e_payment_greedy():
    N = [int(i) for i in list(eval(input()))]
    s = N[::-1] + [0]  # [-d-1]: 最終的に 10**d 円札を何枚受渡ししたかが格納される
    for i in range(len(N)):
        if s[i] >= 6 or (s[i] == 5 and s[i + 1] >= 5):
            s[i] = 10 - s[i]
            s[i + 1] += 1
    return sum(s)


print((e_payment_greedy()))

-def e_payment(): -    N = eval(input()) -    dp = (0, 1)  # 「最下位桁の 1 つ下の桁」を仮想した場合分けを省くための初期化 -    for k, ch in enumerate(N[::-1]):  # 最下位の桁から見る -        n = int(ch) -        t1, t2 = dp -        a = min(t1 + n, t2 + n + 1) -        b = min(t1 + (10 - n), t2 + (10 - (n + 1))) -        dp = (a, b) -    return min(dp[0], dp[1] + 1) +def e_payment_greedy(): +    N = [int(i) for i in list(eval(input()))] +    s = N[::-1] + [0]  # [-d-1]: 最終的に 10**d 円札を何枚受渡ししたかが格納される +    for i in range(len(N)): +        if s[i] >= 6 or (s[i] == 5 and s[i + 1] >= 5): +            s[i] = 10 - s[i] +            s[i + 1] += 1 +    return sum(s) -print((e_payment())) +print((e_payment_greedy()))