import sys
n = [int(i) for i in sys.stdin.readline().rstrip()[::-1]]
ans = 0
i = 0
p = [n[i],10 - n[i]]
now = [0,0]
i += 1
ans = min(p[0], p[1] + 1)
for i in range(1,len(n)):
  now[0] = min(p[0] + n[i]%10, p[1] + n[i]%10 + 1)
  now[1] = min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10)
  ans = min(now[0], now[1] + 1)
  p[0],p[1] = now[0],now[1]
print(ans) import sys sys int(i) for i in sys.stdin.readline().rstrip()[::-1] for i in sys.stdin.readline().rstrip()[::-1] i sys.stdin.readline().rstrip()[::-1] sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip ::-1 -1 for i in sys.stdin.readline().rstrip()[::-1] int(i) int i n = [int(i) for i in sys.stdin.readline().rstrip()[::-1]] n [int(i) for i in sys.stdin.readline().rstrip()[::-1]] ans = 0 ans 0 i = 0 i 0 p = [n[i],10 - n[i]] p [n[i],10 - n[i]] n[i] n i 10 - n[i] 10 n[i] n i now = [0,0] now [0,0] 0 0 i += 1 i 1 ans = min(p[0], p[1] + 1) ans min(p[0], p[1] + 1) min p[0] p 0 p[1] + 1 p[1] p 1 1 for i in range(1,len(n)):
  now[0] = min(p[0] + n[i]%10, p[1] + n[i]%10 + 1)
  now[1] = min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10)
  ans = min(now[0], now[1] + 1)
  p[0],p[1] = now[0],now[1] i range(1,len(n)) range 1 len(n) len n now[0] = min(p[0] + n[i]%10, p[1] + n[i]%10 + 1) now[0] now 0 min(p[0] + n[i]%10, p[1] + n[i]%10 + 1) min p[0] + n[i]%10 p[0] p 0 n[i]%10 n[i] n i 10 p[1] + n[i]%10 + 1 p[1] + n[i]%10 p[1] p 1 n[i]%10 n[i] n i 10 1 now[1] = min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10) now[1] now 1 min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10) min p[0] + 10 - n[i]%10 p[0] + 10 p[0] p 0 10 n[i]%10 n[i] n i 10 p[1] + 9 - n[i]%10 p[1] + 9 p[1] p 1 9 n[i]%10 n[i] n i 10 ans = min(now[0], now[1] + 1) ans min(now[0], now[1] + 1) min now[0] now 0 now[1] + 1 now[1] now 1 1 p[0],p[1] = now[0],now[1] p[0] p 0 now[0] now 0 p[1] p 1 now[1] now 1 print(ans) print ans i += 1 1 i p = [n[i],10 - n[i]] [n[i],10 - n[i]] p i = 0 0 i ans = 0 0 ans ans = min(now[0], now[1] + 1) min(now[0], now[1] + 1) ans ans = min(p[0], p[1] + 1) min(p[0], p[1] + 1) ans n = [int(i) for i in sys.stdin.readline().rstrip()[::-1]] [int(i) for i in sys.stdin.readline().rstrip()[::-1]] n now = [0,0] [0,0] now
import sys
n = [int(i) for i in sys.stdin.readline().rstrip()[::-1]]
ans = 0
i = 0
p = [n[i],10 - n[i]]
now = [0,0]
i += 1
ans = min(p[0], p[1] + 1)
for i in range(1,len(n)):
  now[0] = min(p[0] + n[i]%10, p[1] + n[i]%10 + 1)
  now[1] = min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10)
  ans = min(now[0], now[1] + 1)
  p[0],p[1] = now[0],now[1]
print(ans)
