N=input().strip() LEN=len(N) Y=[0,1,2,3,2,1,2,3,3,2,1] from functools import lru_cache @lru_cache(maxsize=None) def calc(i,k): if i==-1: return k if k==0: x=int(N[i]) else: x=int(N[i])+1 ANS=min(Y[x]+calc(i-1,0),Y[10-x]+calc(i-1,1)) return ANS print(calc(LEN-1,0))