import collections
s = map(int,list(raw_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 collections s = map(int,list(raw_input())) s map(int,list(raw_input())) map int list(raw_input()) list raw_input() raw_input qa = collections.deque([0]) qa collections.deque([0]) collections.deque collections deque [0] 0 qb = collections.deque([1]) qb collections.deque([1]) collections.deque collections deque [1] 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() i range(len(s)) range len(s) len s qa.append(min(qa[0] + s[i], qb[0] + 10 - s[i])) qa.append qa append min(qa[0] + s[i], qb[0] + 10 - s[i]) min qa[0] + s[i] qa[0] qa 0 s[i] s i qb[0] + 10 - s[i] qb[0] + 10 qb[0] qb 0 10 s[i] s i qb.append(min(qa[0] + s[i] + 1, qb[0] + 10 - s[i] - 1)) qb.append qb append min(qa[0] + s[i] + 1, qb[0] + 10 - s[i] - 1) min qa[0] + s[i] + 1 qa[0] + s[i] qa[0] qa 0 s[i] s i 1 qb[0] + 10 - s[i] - 1 qb[0] + 10 - s[i] qb[0] + 10 qb[0] qb 0 10 s[i] s i 1 qa.popleft() qa.popleft qa popleft qb.popleft() qb.popleft qb popleft print qa[-1] qa -1 qa = collections.deque([0]) collections.deque([0]) qa s = map(int,list(raw_input())) map(int,list(raw_input())) s qb = collections.deque([1]) collections.deque([1]) qb
import collections
s = map(int,list(raw_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]



