import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)

# ピッタリ払う
dp0 = [INF] * (N + 1)
# 1 多く払う
dp1 = [INF] * (N + 1)

dp0[0] = 0
dp1[0] = 1
for i, c in enumerate(S):
    dp0[i + 1] = min(dp0[i] + int(c), dp1[i] + 10 - int(c))
    dp1[i + 1] = min(dp0[i] + int(c) + 1, dp1[i] + 10 - int(c) - 1)
# print(dp0)
# print(dp1)
print(dp0[-1]) import os os import sys sys if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r") os.getenv("LOCAL") os.getenv os getenv "LOCAL" sys.stdin = open("_in.txt", "r") sys.stdin sys stdin open("_in.txt", "r") open "_in.txt" "r" sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float("inf") INF float("inf") float "inf" IINF = 10 ** 18 IINF 10 ** 18 10 18 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 S = sys.stdin.buffer.readline().decode().rstrip() S sys.stdin.buffer.readline().decode().rstrip() sys.stdin.buffer.readline().decode().rstrip sys.stdin.buffer.readline().decode() sys.stdin.buffer.readline().decode sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline decode rstrip N = len(S) N len(S) len S dp0 = [INF] * (N + 1) dp0 [INF] * (N + 1) [INF] INF N + 1 N 1 dp1 = [INF] * (N + 1) dp1 [INF] * (N + 1) [INF] INF N + 1 N 1 dp0[0] = 0 dp0[0] dp0 0 0 dp1[0] = 1 dp1[0] dp1 0 1 for i, c in enumerate(S):
    dp0[i + 1] = min(dp0[i] + int(c), dp1[i] + 10 - int(c))
    dp1[i + 1] = min(dp0[i] + int(c) + 1, dp1[i] + 10 - int(c) - 1)
# print(dp0)
# print(dp1) i c enumerate(S) enumerate S dp0[i + 1] = min(dp0[i] + int(c), dp1[i] + 10 - int(c)) dp0[i + 1] dp0 i + 1 i 1 min(dp0[i] + int(c), dp1[i] + 10 - int(c)) min dp0[i] + int(c) dp0[i] dp0 i int(c) int c dp1[i] + 10 - int(c) dp1[i] + 10 dp1[i] dp1 i 10 int(c) int c dp1[i + 1] = min(dp0[i] + int(c) + 1, dp1[i] + 10 - int(c) - 1) dp1[i + 1] dp1 i + 1 i 1 min(dp0[i] + int(c) + 1, dp1[i] + 10 - int(c) - 1) min dp0[i] + int(c) + 1 dp0[i] + int(c) dp0[i] dp0 i int(c) int c 1 dp1[i] + 10 - int(c) - 1 dp1[i] + 10 - int(c) dp1[i] + 10 dp1[i] dp1 i 10 int(c) int c 1 print(dp0[-1]) print dp0[-1] dp0 -1 IINF = 10 ** 18 10 ** 18 IINF dp1 = [INF] * (N + 1) [INF] * (N + 1) dp1 S = sys.stdin.buffer.readline().decode().rstrip() sys.stdin.buffer.readline().decode().rstrip() S dp0 = [INF] * (N + 1) [INF] * (N + 1) dp0 INF = float("inf") float("inf") INF MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD N = len(S) len(S) N
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)

# ピッタリ払う
dp0 = [INF] * (N + 1)
# 1 多く払う
dp1 = [INF] * (N + 1)

dp0[0] = 0
dp1[0] = 1
for i, c in enumerate(S):
    dp0[i + 1] = min(dp0[i] + int(c), dp1[i] + 10 - int(c))
    dp1[i + 1] = min(dp0[i] + int(c) + 1, dp1[i] + 10 - int(c) - 1)
# print(dp0)
# print(dp1)
print(dp0[-1])
