def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans

def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}")
def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans)
main() def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans solve N = [0] + N N [0] + N [0] 0 N k = 0 k 0 ans = 0 ans 0 for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
     d d_next zip(N[::-1], N[-2::-1]) zip N[::-1] N ::-1 -1 N[-2::-1] N -2::-1 -2 -1 d += k d k if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
     d <= 4 or (d==5 and d_next<5) d <= 4 d 4 d==5 and d_next<5 d==5 d 5 d_next<5 d_next 5 k = 0 k 0 ans += d ans d k = 1 k 1 ans += 10 - d ans 10 - d 10 d if k:
        ans += 1
     k ans += 1 ans 1 return ans ans N N def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}") test for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}") i range(N, 10000) range N 10000 rem = i - N rem i - N i N if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}") sum(map(int, str(i))) + sum(map(int, str(rem))) < ans sum(map(int, str(i))) + sum(map(int, str(rem))) sum(map(int, str(i))) sum map(int, str(i)) map int str(i) str i sum(map(int, str(rem))) sum map(int, str(rem)) map int str(rem) str rem ans print(f"!!! N={N}, ans={ans}, trueans={i, rem}") print f"!!! N={N}, ans={ans}, trueans={i, rem}" N N ans ans def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans) main N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input print(solve(N)) print solve(N) solve N main() main def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans solve def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans) def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans) main def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}") def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}") test
def solve(N):
    N = [0] + N
    k = 0  # 繰り上がり
    ans = 0
    for d, d_next in zip(N[::-1], N[-2::-1]):
        d += k
        if d <= 4 or (d==5 and d_next<5):
            k = 0
            ans += d
        else:
            k = 1
            ans += 10 - d
    if k:
        ans += 1
    return ans

def test(N, ans):
    for i in range(N, 10000):
        rem = i - N
        if sum(map(int, str(i))) + sum(map(int, str(rem))) < ans:
            print(f"!!! N={N}, ans={ans}, trueans={i, rem}")
def main():
    N = list(map(int, input()))
    print(solve(N))
    # for i in range(1, 10000):
    #     ans = solve(list(map(int, str(i))))
    #     test(i, ans)
main()
