import sys
input = sys.stdin.readline

def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    return res + flg%2


N = list(map(int, input().strip()))
print(solve(N))

# N = 1
# q = 0
# while True:
#     p = solve(list(map(int, str(N))))
#     if abs(p-q) > 1:
#         print(N, "OK")
#         exit()
#     q = p
#     N += 1 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    return res + flg%2 solve res = 0 res 0 flg = 0 flg 0 for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
     n reversed(N) reversed N if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
         flg == 2 flg 2 if n >= 5:
                flg = 1
            else:
                flg = 0
         n >= 5 n 5 flg = 1 flg 1 flg = 0 flg 0 m = flg + n m flg + n flg n if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
     m == 5 m 5 res += 5 res 5 flg = 2 flg 2 elif m < 5:
            res += m
            flg = 0
         m < 5 m 5 res += m res m flg = 0 flg 0 res += (10 - m) res 10 - m 10 m flg = 1 flg 1 return res + flg%2 res + flg%2 res flg%2 flg 2 N N N = list(map(int, input().strip())) N list(map(int, input().strip())) list map(int, input().strip()) map int input().strip() ().strip () input strip print(solve(N)) print solve(N) solve N input = sys.stdin.readline sys.stdin.readline input def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    return res + flg%2 def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    return res + flg%2 solve N = list(map(int, input().strip())) list(map(int, input().strip())) N
import sys
input = sys.stdin.readline

def solve(N):
    res = 0
    flg = 0
    for n in reversed(N):
        if flg == 2:
            if n >= 5:
                flg = 1
            else:
                flg = 0
        m = flg + n
        if m == 5:
            res += 5
            flg = 2
        elif m < 5:
            res += m
            flg = 0
        else:
            res += (10 - m)
            flg = 1
    return res + flg%2


N = list(map(int, input().strip()))
print(solve(N))

# N = 1
# q = 0
# while True:
#     p = solve(list(map(int, str(N))))
#     if abs(p-q) > 1:
#         print(N, "OK")
#         exit()
#     q = p
#     N += 1