import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main_b():
	s = input()
	pp=0
	na=0
	for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	print(pp)
	
def main():
	s = input()
	pmin=1000
	mmin=0
	s='0'+s
	for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	return min(pmin, mmin)
	
	
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret) import sys sys input_methods=['clipboard','file','key'] input_methods ['clipboard','file','key'] 'clipboard' 'file' 'key' using_method=0 using_method 0 input_method=input_methods[using_method] input_method input_methods[using_method] input_methods using_method IN=lambda : map(int, input().split()) IN lambda : map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split mod=1000000007 mod 1000000007 def main_b():
	s = input()
	pp=0
	na=0
	for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	print(pp)
	 main_b s = input() s input() input pp=0 pp 0 na=0 na 0 for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	 i c enumerate(s[::-1]) enumerate s[::-1] s ::-1 -1 cc=na+int(c) cc na+int(c) na int(c) int c na=0 na 0 if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	 cc <= 4 cc 4 pp+=cc pp cc na = 1 na 1 if i==len(s)-1:
				pp+=1
			 i==len(s)-1 i len(s)-1 len(s) len s 1 pp+=1 pp 1 pp+=10-cc pp 10-cc 10 cc print(pp) print pp def main():
	s = input()
	pmin=1000
	mmin=0
	s='0'+s
	for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	return min(pmin, mmin)
	
	
	
	
	
	
#+++++ main s = input() s input() input pmin=1000 pmin 1000 mmin=0 mmin 0 s='0'+s s '0'+s '0' s for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	 c s[::-1] s ::-1 -1 v = int(c) v int(c) int c npmin = min(pmin + 10 - (v+1), mmin + 10 - v) npmin min(pmin + 10 - (v+1), mmin + 10 - v) min pmin + 10 - (v+1) pmin + 10 pmin 10 v+1 v 1 mmin + 10 - v mmin + 10 mmin 10 v nmmin = min(pmin + v + 1, mmin + v) nmmin min(pmin + v + 1, mmin + v) min pmin + v + 1 pmin + v pmin v 1 mmin + v mmin v pmin = npmin pmin npmin mmin = nmmin mmin nmmin return min(pmin, mmin) min(pmin, mmin) min pmin mmin isTest=False isTest False def pa(v):
	if isTest:
		print(v)
		 pa if isTest:
		print(v)
		 isTest print(v) print v v v def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l input_clipboard import clipboard clipboard input_text=clipboard.get() input_text clipboard.get() clipboard.get clipboard get input_l=input_text.splitlines() input_l input_text.splitlines() input_text.splitlines input_text splitlines for l in input_l:
		yield l l input_l yield l l if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret) __name__ == "__main__" __name__ "__main__" if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	 sys.platform =='ios' sys.platform sys platform 'ios' if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		 input_method==input_methods[0] input_method input_methods[0] input_methods 0 ic=input_clipboard() ic input_clipboard() input_clipboard input = lambda : ic.__next__() input lambda : ic.__next__() ic.__next__() ic.__next__ ic __next__ elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		 input_method==input_methods[1] input_method input_methods[1] input_methods 1 sys.stdin=open('inputFile.txt') sys.stdin sys stdin open('inputFile.txt') open 'inputFile.txt' isTest=True isTest True ret = main() ret main() main if ret is not None:
		print(ret) ret is not None ret None print(ret) print ret isTest=True True isTest def main_b():
	s = input()
	pp=0
	na=0
	for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	print(pp)
	 def main_b():
	s = input()
	pp=0
	na=0
	for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	print(pp)
	 main_b ic=input_clipboard() input_clipboard() ic isTest=False False isTest input = lambda : ic.__next__() lambda : ic.__next__() input def pa(v):
	if isTest:
		print(v)
		 def pa(v):
	if isTest:
		print(v)
		 pa ret = main() main() ret mod=1000000007 1000000007 mod input_methods=['clipboard','file','key'] ['clipboard','file','key'] input_methods using_method=0 0 using_method IN=lambda : map(int, input().split()) lambda : map(int, input().split()) IN input_method=input_methods[using_method] input_methods[using_method] input_method def main():
	s = input()
	pmin=1000
	mmin=0
	s='0'+s
	for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	return min(pmin, mmin)
	
	
	
	
	
	
#+++++ def main():
	s = input()
	pmin=1000
	mmin=0
	s='0'+s
	for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	return min(pmin, mmin)
	
	
	
	
	
	
#+++++ main def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l input_clipboard
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main_b():
	s = input()
	pp=0
	na=0
	for i, c in enumerate(s[::-1]):
		cc=na+int(c)
		na=0
		if cc <= 4:
			pp+=cc
		else:
			na = 1
			if i==len(s)-1:
				pp+=1
			pp+=10-cc
			
	print(pp)
	
def main():
	s = input()
	pmin=1000
	mmin=0
	s='0'+s
	for c in s[::-1]:
		v = int(c)
		npmin = min(pmin + 10 - (v+1), mmin + 10 - v)
		nmmin = min(pmin + v + 1, mmin + v)
		pmin = npmin
		mmin = nmmin
	return min(pmin, mmin)
	
	
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)