n = input()
a = 0
b = 2
for s in n:
  v = int(s)
  a_ = min(a + v, b + v)
  b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
  a = a_
  b = b_
  
print(min(a, b)) n = input() n input() input a = 0 a 0 b = 2 b 2 for s in n:
  v = int(s)
  a_ = min(a + v, b + v)
  b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
  a = a_
  b = b_
   s n v = int(s) v int(s) int s a_ = min(a + v, b + v) a_ min(a + v, b + v) min a + v a v b + v b v b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2) b_ min(a + 10 - v + 1, b + 10 - v + 1 - 2) min a + 10 - v + 1 a + 10 - v a + 10 a 10 v 1 b + 10 - v + 1 - 2 b + 10 - v + 1 b + 10 - v b + 10 b 10 v 1 2 a = a_ a a_ b = b_ b b_ print(min(a, b)) print min(a, b) min a b a = 0 0 a v = int(s) int(s) v a_ = min(a + v, b + v) min(a + v, b + v) a_ n = input() input() n b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2) min(a + 10 - v + 1, b + 10 - v + 1 - 2) b_ b = 2 2 b b = b_ b_ b a = a_ a_ a
n = input()
a = 0
b = 2
for s in n:
  v = int(s)
  a_ = min(a + v, b + v)
  b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
  a = a_
  b = b_
  
print(min(a, b))