d,e=0,0
for x in list("0"+input()):
	d,e=int(x)+min(e,d),9-int(x)+min(e,d+2)
print(min(d,e)) d,e=0,0 d 0 e 0 for x in list("0"+input()):
	d,e=int(x)+min(e,d),9-int(x)+min(e,d+2) x list("0"+input()) list "0"+input() "0" input() input d,e=int(x)+min(e,d),9-int(x)+min(e,d+2) d int(x)+min(e,d) int(x) int x min(e,d) min e d e 9-int(x)+min(e,d+2) 9-int(x) 9 int(x) int x min(e,d+2) min e d+2 d 2 print(min(d,e)) print min(d,e) min d e e=0,0 0 e d,e=0,0 0 d d,e=int(x)+min(e,d),9-int(x)+min(e,d+2) int(x)+min(e,d) d e=int(x)+min(e,d),9-int(x)+min(e,d+2) 9-int(x)+min(e,d+2) e
d,e=0,0
for x in list("0"+input()):
	d,e=int(x)+min(e,d),9-int(x)+min(e,d+2)
print(min(d,e))