import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    just, less = 0, INF
    for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
    print(min(just, less + 1))
resolve() import sys sys sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 INF=float("inf") INF float("inf") float "inf" MOD=10**9+7 MOD 10**9+7 10**9 10 9 7 input=lambda:sys.stdin.readline().rstrip() input lambda:sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def resolve():
    just, less = 0, INF
    for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
    print(min(just, less + 1)) resolve just, less = 0, INF just 0 less INF for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
     d input()[::-1] () input ::-1 -1 d = int(d) d int(d) int d njust = min(just + d, less + d + 1) njust min(just + d, less + d + 1) min just + d just d less + d + 1 less + d less d 1 nless = min(just + (10-d), less + (9-d)) nless min(just + (10-d), less + (9-d)) min just + (10-d) just 10-d 10 d less + (9-d) less 9-d 9 d just, less = njust, nless just njust less nless print(min(just, less + 1)) print min(just, less + 1) min just less + 1 less 1 resolve() resolve INF=float("inf") float("inf") INF MOD=10**9+7 10**9+7 MOD input=lambda:sys.stdin.readline().rstrip() lambda:sys.stdin.readline().rstrip() input def resolve():
    just, less = 0, INF
    for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
    print(min(just, less + 1)) def resolve():
    just, less = 0, INF
    for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
    print(min(just, less + 1)) resolve
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    just, less = 0, INF
    for d in input()[::-1]:
        d = int(d)
        njust = min(just + d, less + d + 1)
        nless = min(just + (10-d), less + (9-d))
        just, less = njust, nless
    print(min(just, less + 1))
resolve()