# coding: utf-8
import sys

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

N = sr()
dp = [0, 1]  # ちょうど払う最小枚数、１円多くはらう最小枚数
for x in N:
    x = int(x)
    prev = dp[:]
    dp[0] = min(prev[0]+x, prev[1]+(10-x))
    dp[1] = min(prev[0]+x+1, prev[1]+(10-x-1))

answer = dp[0]
print(answer)
# 33 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 = sr() N sr() sr dp = [0, 1] dp [0, 1] 0 1 for x in N:
    x = int(x)
    prev = dp[:]
    dp[0] = min(prev[0]+x, prev[1]+(10-x))
    dp[1] = min(prev[0]+x+1, prev[1]+(10-x-1)) x N x = int(x) x int(x) int x prev = dp[:] prev dp[:] dp : dp[0] = min(prev[0]+x, prev[1]+(10-x)) dp[0] dp 0 min(prev[0]+x, prev[1]+(10-x)) min prev[0]+x prev[0] prev 0 x prev[1]+(10-x) prev[1] prev 1 10-x 10 x dp[1] = min(prev[0]+x+1, prev[1]+(10-x-1)) dp[1] dp 1 min(prev[0]+x+1, prev[1]+(10-x-1)) min prev[0]+x+1 prev[0]+x prev[0] prev 0 x 1 prev[1]+(10-x-1) prev[1] prev 1 10-x-1 10-x 10 x 1 answer = dp[0] answer dp[0] dp 0 print(answer) print answer dp = [0, 1] [0, 1] dp lr = lambda: list(map(int, sr().split())) lambda: list(map(int, sr().split())) lr x = int(x) int(x) x ir = lambda: int(sr()) lambda: int(sr()) ir N = sr() sr() N answer = dp[0] dp[0] answer sr = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() sr prev = dp[:] dp[:] prev
# coding: utf-8
import sys

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

N = sr()
dp = [0, 1]  # ちょうど払う最小枚数、１円多くはらう最小枚数
for x in N:
    x = int(x)
    prev = dp[:]
    dp[0] = min(prev[0]+x, prev[1]+(10-x))
    dp[1] = min(prev[0]+x+1, prev[1]+(10-x-1))

answer = dp[0]
print(answer)
# 33