s = input()
n = len(s)

ans = 0

sub = 0
judge = False
for i in range(n-1, -1, -1):
  key = int(s[i])+sub
  if key <= 4:
    ans += key
    sub = 0
  elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
  else:
    ans += 10-key
    sub = 1
ans += sub
print(ans) s = input() s input() input n = len(s) n len(s) len s ans = 0 ans 0 sub = 0 sub 0 judge = False judge False for i in range(n-1, -1, -1):
  key = int(s[i])+sub
  if key <= 4:
    ans += key
    sub = 0
  elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
  else:
    ans += 10-key
    sub = 1 i range(n-1, -1, -1) range n-1 n 1 -1 -1 key = int(s[i])+sub key int(s[i])+sub int(s[i]) int s[i] s i sub if key <= 4:
    ans += key
    sub = 0
  elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
  else:
    ans += 10-key
    sub = 1 key <= 4 key 4 ans += key ans key sub = 0 sub 0 elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
   key == 5 key 5 ans += 5 ans 5 if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
   i == 0 i 0 sub = 0 sub 0 elif int(s[i-1]) <= 4:
      sub = 0
     int(s[i-1]) <= 4 int(s[i-1]) int s[i-1] s i-1 i 1 4 sub = 0 sub 0 sub = 1 sub 1 ans += 10-key ans 10-key 10 key sub = 1 sub 1 ans += sub ans sub print(ans) print ans n = len(s) len(s) n ans += 10-key 10-key ans ans += key key ans sub = 0 0 sub s = input() input() s sub = 1 1 sub ans += sub sub ans sub = 0 0 sub judge = False False judge ans += 5 5 ans sub = 0 0 sub sub = 0 0 sub ans = 0 0 ans key = int(s[i])+sub int(s[i])+sub key sub = 1 1 sub
s = input()
n = len(s)

ans = 0

sub = 0
judge = False
for i in range(n-1, -1, -1):
  key = int(s[i])+sub
  if key <= 4:
    ans += key
    sub = 0
  elif key == 5:
    ans += 5
    if i == 0:
      sub = 0
    elif int(s[i-1]) <= 4:
      sub = 0
    else:
      sub = 1
  else:
    ans += 10-key
    sub = 1
ans += sub
print(ans)