N=input()
N=N[::-1]+"0"
ans=0
up=0
for i,n in enumerate(N):
  d=int(n)+up
  if d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5):
    ans+=(10-d)
    up=1
  else:
    ans+=d
    up=0
print(ans) N=input() N input() input N=N[::-1]+"0" N N[::-1]+"0" N[::-1] N ::-1 -1 "0" ans=0 ans 0 up=0 up 0 for i,n in enumerate(N):
  d=int(n)+up
  if d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5):
    ans+=(10-d)
    up=1
  else:
    ans+=d
    up=0 i n enumerate(N) enumerate N d=int(n)+up d int(n)+up int(n) int n up if d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5):
    ans+=(10-d)
    up=1
  else:
    ans+=d
    up=0 d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5) d>5 d 5 d==5 and i<len(N)-1 and int(N[i+1])>=5 d==5 and i<len(N)-1 d==5 d 5 i<len(N)-1 i len(N)-1 len(N) len N 1 int(N[i+1])>=5 int(N[i+1]) int N[i+1] N i+1 i 1 5 ans+=(10-d) ans 10-d 10 d up=1 up 1 ans+=d ans d up=0 up 0 print(ans) print ans up=1 1 up N=input() input() N up=0 0 up d=int(n)+up int(n)+up d N=N[::-1]+"0" N[::-1]+"0" N ans+=(10-d) 10-d ans ans+=d d ans ans=0 0 ans up=0 0 up
N=input()
N=N[::-1]+"0"
ans=0
up=0
for i,n in enumerate(N):
  d=int(n)+up
  if d>5 or (d==5 and i<len(N)-1 and int(N[i+1])>=5):
    ans+=(10-d)
    up=1
  else:
    ans+=d
    up=0
print(ans)