import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = list(map(int, input()))

def solve(N):
    dp = (0, 1) # n円の場合とn+1円の場合
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
    return dp[0]

print(solve(N)) import sys sys sr = lambda: sys.stdin.readline().rstrip() sr 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 ir = lambda: int(sr()) ir lambda: int(sr()) int(sr()) int sr() sr lr = lambda: list(map(int, sr().split())) lr lambda: list(map(int, sr().split())) list(map(int, sr().split())) list map(int, sr().split()) map int sr().split() ().split () sr split N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input def solve(N):
    dp = (0, 1) # n円の場合とn+1円の場合
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
    return dp[0] solve dp = (0, 1) dp (0, 1) 0 1 for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
     n N a = min(dp[0] + n, dp[1] + 10 - n) a min(dp[0] + n, dp[1] + 10 - n) min dp[0] + n dp[0] dp 0 n dp[1] + 10 - n dp[1] + 10 dp[1] dp 1 10 n b = min(dp[0] + n + 1, dp[1] + 10 - (n+1)) b min(dp[0] + n + 1, dp[1] + 10 - (n+1)) min dp[0] + n + 1 dp[0] + n dp[0] dp 0 n 1 dp[1] + 10 - (n+1) dp[1] + 10 dp[1] dp 1 10 n+1 n 1 dp = (a, b) dp (a, b) a b return dp[0] dp[0] dp 0 N N print(solve(N)) print solve(N) solve N sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr def solve(N):
    dp = (0, 1) # n円の場合とn+1円の場合
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
    return dp[0] def solve(N):
    dp = (0, 1) # n円の場合とn+1円の場合
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
    return dp[0] solve N = list(map(int, input())) list(map(int, input())) N lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr ir = lambda: int(sr()) lambda: int(sr()) ir
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = list(map(int, input()))

def solve(N):
    dp = (0, 1) # n円の場合とn+1円の場合
    for n in N:
        a = min(dp[0] + n, dp[1] + 10 - n)
        b = min(dp[0] + n + 1, dp[1] + 10 - (n+1))
        dp = (a, b)
    return dp[0]

print(solve(N))
