import sys

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ns()

ans = [0, 1000000000]
kuri = [0, 0]
five = 0
for n in N[::-1]:
    n = int(n)
    # print(ans)
    ans_old = [ans[0], ans[1]]
    ans[0] = min(ans_old[0] + n, ans_old[1] + n + 1)
    ans[1] = min(ans_old[0] + 10 - n, ans_old[1] + 9 - n)
print(min(ans[0], ans[1] + 1)) import sys sys stdin = sys.stdin stdin sys.stdin sys stdin def ni(): return int(ns()) ni def na(): return list(map(int, stdin.readline().split())) na def ns(): return stdin.readline().rstrip()  # ignore trailing spaces ns N = ns() N ns() ns ans = [0, 1000000000] ans [0, 1000000000] 0 1000000000 kuri = [0, 0] kuri [0, 0] 0 0 five = 0 five 0 for n in N[::-1]:
    n = int(n)
    # print(ans)
    ans_old = [ans[0], ans[1]]
    ans[0] = min(ans_old[0] + n, ans_old[1] + n + 1)
    ans[1] = min(ans_old[0] + 10 - n, ans_old[1] + 9 - n) n N[::-1] N ::-1 -1 n = int(n) n int(n) int n ans_old = [ans[0], ans[1]] ans_old [ans[0], ans[1]] ans[0] ans 0 ans[1] ans 1 ans[0] = min(ans_old[0] + n, ans_old[1] + n + 1) ans[0] ans 0 min(ans_old[0] + n, ans_old[1] + n + 1) min ans_old[0] + n ans_old[0] ans_old 0 n ans_old[1] + n + 1 ans_old[1] + n ans_old[1] ans_old 1 n 1 ans[1] = min(ans_old[0] + 10 - n, ans_old[1] + 9 - n) ans[1] ans 1 min(ans_old[0] + 10 - n, ans_old[1] + 9 - n) min ans_old[0] + 10 - n ans_old[0] + 10 ans_old[0] ans_old 0 10 n ans_old[1] + 9 - n ans_old[1] + 9 ans_old[1] ans_old 1 9 n print(min(ans[0], ans[1] + 1)) print min(ans[0], ans[1] + 1) min ans[0] ans 0 ans[1] + 1 ans[1] ans 1 1 kuri = [0, 0] [0, 0] kuri five = 0 0 five ans = [0, 1000000000] [0, 1000000000] ans N = ns() ns() N def ni(): return int(ns()) def ni(): return int(ns()) ni n = int(n) int(n) n ans_old = [ans[0], ans[1]] [ans[0], ans[1]] ans_old stdin = sys.stdin sys.stdin stdin def ns(): return stdin.readline().rstrip()  # ignore trailing spaces def ns(): return stdin.readline().rstrip()  # ignore trailing spaces ns def na(): return list(map(int, stdin.readline().split())) def na(): return list(map(int, stdin.readline().split())) na
import sys

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ns()

ans = [0, 1000000000]
kuri = [0, 0]
five = 0
for n in N[::-1]:
    n = int(n)
    # print(ans)
    ans_old = [ans[0], ans[1]]
    ans[0] = min(ans_old[0] + n, ans_old[1] + n + 1)
    ans[1] = min(ans_old[0] + 10 - n, ans_old[1] + 9 - n)
print(min(ans[0], ans[1] + 1))
