N = list(map(int, input()))

m = 0 # 最小の枚数
m_ = 1 # 繰り上がり時の枚数

for n in N:
  m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1))
print(m) N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input m = 0 m 0 m_ = 1 m_ 1 for n in N:
  m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1)) n N m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1)) m min(m+n, m_ + 10-n) min m+n m n m_ + 10-n m_ + 10 m_ 10 n m_ min(m+n + 1, m_ + 10-(n+1)) min m+n + 1 m+n m n 1 m_ + 10-(n+1) m_ + 10 m_ 10 n+1 n 1 print(m) print m m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1)) min(m+n, m_ + 10-n) m m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1)) min(m+n + 1, m_ + 10-(n+1)) m_ m_ = 1 1 m_ N = list(map(int, input())) list(map(int, input())) N m = 0 0 m
N = list(map(int, input()))

m = 0 # 最小の枚数
m_ = 1 # 繰り上がり時の枚数

for n in N:
  m, m_ = min(m+n, m_ + 10-n), min(m+n + 1, m_ + 10-(n+1))
print(m)