def e_payment():
    N = 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():
    N = 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) e_payment N = input() N input() input dp = (0, 1) dp (0, 1) 0 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)
     k ch enumerate(N[::-1]) enumerate N[::-1] N ::-1 -1 n = int(ch) n int(ch) int ch t1, t2 = dp t1 dp t2 a = min(t1 + n, t2 + n + 1) a min(t1 + n, t2 + n + 1) min t1 + n t1 n t2 + n + 1 t2 + n t2 n 1 b = min(t1 + (10 - n), t2 + (10 - (n + 1))) b min(t1 + (10 - n), t2 + (10 - (n + 1))) min t1 + (10 - n) t1 10 - n 10 n t2 + (10 - (n + 1)) t2 10 - (n + 1) 10 n + 1 n 1 dp = (a, b) dp (a, b) a b return min(dp[0], dp[1] + 1) min(dp[0], dp[1] + 1) min dp[0] dp 0 dp[1] + 1 dp[1] dp 1 1 print(e_payment()) print e_payment() e_payment def e_payment():
    N = 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():
    N = 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) e_payment
def e_payment():
    N = 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())