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

dp = [[0,0] for _ in range(len(s))]

for i in range(len(dp)):

	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i]

	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i]

	dp[i][0] = min(a,b)



	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1

	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1

	dp[i][1] = min(a,b)

print(dp[-1][0])







import collections

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

qa = collections.deque([0])

qb = collections.deque([1])

for i in range(len(s)):

	qa.append(min(qa[0] + s[i], qb[0] + 10 - s[i]))

	qb.append(min(qa[0] + s[i] + 1, qb[0] + 10 - s[i] - 1))

	qa.popleft()

	qb.popleft()



print(qa[-1])







s = list(map(int, list(input())))
dp = [[0, 0] for _ in range(len(s))]
for i in range(len(dp)):
    a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i]
    b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i]
    dp[i][0] = min(a, b)
    a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1
    b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1
    dp[i][1] = min(a, b)
print(dp[-1][0])

import collections

s = list(map(int, list(input())))
qa = collections.deque([0])
qb = collections.deque([1])
for i in range(len(s)):
    qa.append(min(qa[0] + s[i], qb[0] + 10 - s[i]))
    qb.append(min(qa[0] + s[i] + 1, qb[0] + 10 - s[i] - 1))
    qa.popleft()
    qb.popleft()
print(qa[-1])

+import collections + -dp = [[0, 0] for _ in range(len(s))] -for i in range(len(dp)): -    a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] -    b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] -    dp[i][0] = min(a, b) -    a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1 -    b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1 -    dp[i][1] = min(a, b) -print(dp[-1][0]) +qa = collections.deque([0]) +qb = collections.deque([1]) +for i in range(len(s)): +    qa.append(min(qa[0] + s[i], qb[0] + 10 - s[i])) +    qb.append(min(qa[0] + s[i] + 1, qb[0] + 10 - s[i] - 1)) +    qa.popleft() +    qb.popleft() +print(qa[-1])