import sys
import functools
# sys.setrecursionlimit(100000000)
n = input()
# import random
# n = ''.join(str(random.randint(1,9)) for _ in range(10**6))

#
# @functools.lru_cache(None)
# def doit(index: int, carry: int) -> int:
#     if index >= len(n):
#         return 0 if carry == 0 else 2
#     d = int(n[index])
#     if carry > 0:
#         return min(10 - d + doit(index + 1, 0), 9 - d + doit(index + 1, 1))
#     else:
#         return min(d + doit(index + 1, 0), d + 1 + doit(index + 1, 1))
#
#
# print(min(doit(0, 0), 1 + doit(0, 1)))


dp = [0, 2]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

print(min(dp[0], 1 + dp[1])) import sys sys import functools functools n = input() n input() input dp = [0, 2] dp [0, 2] 0 2 for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] d reversed(n) reversed n d = int(d) d int(d) int d dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] dp [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] min(d + dp[0], d + 1 + dp[1]) min d + dp[0] d dp[0] dp 0 d + 1 + dp[1] d + 1 d 1 dp[1] dp 1 min(10 - d + dp[0], 9 - d + dp[1]) min 10 - d + dp[0] 10 - d 10 d dp[0] dp 0 9 - d + dp[1] 9 - d 9 d dp[1] dp 1 print(min(dp[0], 1 + dp[1])) print min(dp[0], 1 + dp[1]) min dp[0] dp 0 1 + dp[1] 1 dp[1] dp 1 n = input() input() n d = int(d) int(d) d dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] dp dp = [0, 2] [0, 2] dp
import sys
import functools
# sys.setrecursionlimit(100000000)
n = input()
# import random
# n = ''.join(str(random.randint(1,9)) for _ in range(10**6))

#
# @functools.lru_cache(None)
# def doit(index: int, carry: int) -> int:
#     if index >= len(n):
#         return 0 if carry == 0 else 2
#     d = int(n[index])
#     if carry > 0:
#         return min(10 - d + doit(index + 1, 0), 9 - d + doit(index + 1, 1))
#     else:
#         return min(d + doit(index + 1, 0), d + 1 + doit(index + 1, 1))
#
#
# print(min(doit(0, 0), 1 + doit(0, 1)))


dp = [0, 2]
for d in reversed(n):
    d = int(d)
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

print(min(dp[0], 1 + dp[1]))
