import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	n=RD()[::-1]
	N=list(map(int,n))+[0]
	#print(N)

	ans=0

	for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	print(ans)









if __name__ == "__main__":
	main() import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline def RD(): return input().rstrip().decode() RD def II(): return int(input()) II def FI(): return int(input()) FI def MI(): return map(int,input().split()) MI def MF(): return map(float,input().split()) MF def LI(): return list(map(int,input().split())) LI def LF(): return list(map(float,input().split())) LF def TI(): return tuple(map(int,input().split()))
# rstrip().decode() TI def main():
	n=RD()[::-1]
	N=list(map(int,n))+[0]
	#print(N)

	ans=0

	for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	print(ans) main n=RD()[::-1] n RD()[::-1] () RD ::-1 -1 N=list(map(int,n))+[0] N list(map(int,n))+[0] list(map(int,n)) list map(int,n) map int n [0] 0 ans=0 ans 0 for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	 i range(len(n)+1) range len(n)+1 len(n) len n 1 if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	 N[i]<=4 N[i] N i 4 ans+=N[i] ans N[i] N i elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		 N[i]>=6 N[i] N i 6 ans+=10-N[i] ans 10-N[i] 10 N[i] N i N[i+1]+=1 N[i+1] N i+1 i 1 1 if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	 N[i+1]<=4 N[i+1] N i+1 i 1 4 ans+=N[i] ans N[i] N i ans+=N[i] ans N[i] N i N[i+1]+=1 N[i+1] N i+1 i 1 1 print(ans) print ans if __name__ == "__main__":
	main() __name__ == "__main__" __name__ "__main__" main() main def main():
	n=RD()[::-1]
	N=list(map(int,n))+[0]
	#print(N)

	ans=0

	for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	print(ans) def main():
	n=RD()[::-1]
	N=list(map(int,n))+[0]
	#print(N)

	ans=0

	for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	print(ans) main input = sys.stdin.buffer.readline sys.stdin.buffer.readline input def II(): return int(input()) def II(): return int(input()) II def LF(): return list(map(float,input().split())) def LF(): return list(map(float,input().split())) LF def LI(): return list(map(int,input().split())) def LI(): return list(map(int,input().split())) LI def MF(): return map(float,input().split()) def MF(): return map(float,input().split()) MF def MI(): return map(int,input().split()) def MI(): return map(int,input().split()) MI def TI(): return tuple(map(int,input().split()))
# rstrip().decode() def TI(): return tuple(map(int,input().split()))
# rstrip().decode() TI def FI(): return int(input()) def FI(): return int(input()) FI def RD(): return input().rstrip().decode() def RD(): return input().rstrip().decode() RD
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	n=RD()[::-1]
	N=list(map(int,n))+[0]
	#print(N)

	ans=0

	for i in range(len(n)+1):
		if N[i]<=4:
			ans+=N[i]
		elif N[i]>=6:
			ans+=10-N[i]
			N[i+1]+=1
		else:
			if N[i+1]<=4:
				ans+=N[i]
			else:
				ans+=N[i]
				N[i+1]+=1

	print(ans)









if __name__ == "__main__":
	main()
