def solve():
    def min2(x, y): return x if x <= y else y

    strN = '0' + input()

    Ns = list(map(int, strN))

    dp = [0, float('inf')]
    for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

    print(dp[0])


solve() def solve():
    def min2(x, y): return x if x <= y else y

    strN = '0' + input()

    Ns = list(map(int, strN))

    dp = [0, float('inf')]
    for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

    print(dp[0]) solve def min2(x, y): return x if x <= y else y

     min2 x x y y strN = '0' + input() strN '0' + input() '0' input() input Ns = list(map(int, strN)) Ns list(map(int, strN)) list map(int, strN) map int strN dp = [0, float('inf')] dp [0, float('inf')] 0 float('inf') float 'inf' for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

     Ni reversed(Ns) reversed Ns dp2 = [0, 0] dp2 [0, 0] 0 0 if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
         Ni == 9 Ni 9 dp2[0] = dp[0] + Ni dp2[0] dp2 0 dp[0] + Ni dp[0] dp 0 Ni dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1) dp2[0] dp2 0 min2(dp[0] + Ni, dp[1] + Ni + 1) min2 dp[0] + Ni dp[0] dp 0 Ni dp[1] + Ni + 1 dp[1] + Ni dp[1] dp 1 Ni 1 dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni) dp2[1] dp2 1 min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni) min2 dp[0] + 10 - Ni dp[0] + 10 dp[0] dp 0 10 Ni dp[1] + 9 - Ni dp[1] + 9 dp[1] dp 1 9 Ni dp = dp2 dp dp2 print(dp[0]) print dp[0] dp 0 solve() solve def solve():
    def min2(x, y): return x if x <= y else y

    strN = '0' + input()

    Ns = list(map(int, strN))

    dp = [0, float('inf')]
    for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

    print(dp[0]) def solve():
    def min2(x, y): return x if x <= y else y

    strN = '0' + input()

    Ns = list(map(int, strN))

    dp = [0, float('inf')]
    for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

    print(dp[0]) solve
def solve():
    def min2(x, y): return x if x <= y else y

    strN = '0' + input()

    Ns = list(map(int, strN))

    dp = [0, float('inf')]
    for Ni in reversed(Ns):
        dp2 = [0, 0]
        if Ni == 9:
            dp2[0] = dp[0] + Ni
        else:
            dp2[0] = min2(dp[0] + Ni, dp[1] + Ni + 1)
        dp2[1] = min2(dp[0] + 10 - Ni, dp[1] + 9 - Ni)
        dp = dp2

    print(dp[0])


solve()
