import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()


n = ns()
n = list(map(int,n[::-1])) + [0]

ans = 0

for i in range(len(n)):
    a = n[i]
    if a <= 4:
        ans += a
    elif a >= 6:
        ans += 10-a
        n[i+1] += 1
    else:
        if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1

print(ans) import sys sys stdin = sys.stdin stdin sys.stdin sys stdin sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 ni = lambda: int(ns()) ni lambda: int(ns()) int(ns()) int ns() ns na = lambda: list(map(int, stdin.readline().split())) na lambda: list(map(int, stdin.readline().split())) list(map(int, stdin.readline().split())) list map(int, stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split nn = lambda: list(stdin.readline().split()) nn lambda: list(stdin.readline().split()) list(stdin.readline().split()) list stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split ns = lambda: stdin.readline().rstrip() ns lambda: stdin.readline().rstrip() stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip n = ns() n ns() ns n = list(map(int,n[::-1])) + [0] n list(map(int,n[::-1])) + [0] list(map(int,n[::-1])) list map(int,n[::-1]) map int n[::-1] n ::-1 -1 [0] 0 ans = 0 ans 0 for i in range(len(n)):
    a = n[i]
    if a <= 4:
        ans += a
    elif a >= 6:
        ans += 10-a
        n[i+1] += 1
    else:
        if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1 i range(len(n)) range len(n) len n a = n[i] a n[i] n i if a <= 4:
        ans += a
    elif a >= 6:
        ans += 10-a
        n[i+1] += 1
    else:
        if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1 a <= 4 a 4 ans += a ans a elif a >= 6:
        ans += 10-a
        n[i+1] += 1
     a >= 6 a 6 ans += 10-a ans 10-a 10 a n[i+1] += 1 n[i+1] n i+1 i 1 1 if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1 n[i+1] <= 4 n[i+1] n i+1 i 1 4 ans += a ans a ans += a ans a n[i+1] += 1 n[i+1] n i+1 i 1 1 print(ans) print ans ans += a a ans n = list(map(int,n[::-1])) + [0] list(map(int,n[::-1])) + [0] n ans += a a ans ni = lambda: int(ns()) lambda: int(ns()) ni na = lambda: list(map(int, stdin.readline().split())) lambda: list(map(int, stdin.readline().split())) na ans += 10-a 10-a ans nn = lambda: list(stdin.readline().split()) lambda: list(stdin.readline().split()) nn ans = 0 0 ans stdin = sys.stdin sys.stdin stdin ans += a a ans a = n[i] n[i] a ns = lambda: stdin.readline().rstrip() lambda: stdin.readline().rstrip() ns n = ns() ns() n
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()


n = ns()
n = list(map(int,n[::-1])) + [0]

ans = 0

for i in range(len(n)):
    a = n[i]
    if a <= 4:
        ans += a
    elif a >= 6:
        ans += 10-a
        n[i+1] += 1
    else:
        if n[i+1] <= 4:
            ans += a
        else:
            ans += a
            n[i+1] += 1

print(ans)