def e_payment_greedy():
    N = [int(i) for i in list(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_greedy():
    N = [int(i) for i in list(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) e_payment_greedy int(i) for i in list(input()) for i in list(input()) i list(input()) list input() input for i in list(input()) int(i) int i N = [int(i) for i in list(input())] N [int(i) for i in list(input())] s = N[::-1] + [0] s N[::-1] + [0] N[::-1] N ::-1 -1 [0] 0 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
     i range(len(N)) range len(N) len N if s[i] >= 6 or (s[i] == 5 and s[i + 1] >= 5):
            s[i] = 10 - s[i]
            s[i + 1] += 1
     s[i] >= 6 or (s[i] == 5 and s[i + 1] >= 5) s[i] >= 6 s[i] s i 6 s[i] == 5 and s[i + 1] >= 5 s[i] == 5 s[i] s i 5 s[i + 1] >= 5 s[i + 1] s i + 1 i 1 5 s[i] = 10 - s[i] s[i] s i 10 - s[i] 10 s[i] s i s[i + 1] += 1 s[i + 1] s i + 1 i 1 1 return sum(s) sum(s) sum s print(e_payment_greedy()) print e_payment_greedy() e_payment_greedy def e_payment_greedy():
    N = [int(i) for i in list(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) def e_payment_greedy():
    N = [int(i) for i in list(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) e_payment_greedy
def e_payment_greedy():
    N = [int(i) for i in list(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())