s = input()
nums = list(reversed([int(c) for c in s])) + [0]
r = (0, 1) # no borrow, with borrow
for i in range(len(nums)):
    # print(r)
    next_no_borrow = min(r[0] + nums[i], r[1] + nums[i] + 1)
    next_borrow = min(10 - nums[i] + r[0], 9 - nums[i] + r[1])
    r = (next_no_borrow, next_borrow)
print(min(r)) s = input() s input() input int(c) for c in s for c in s c s for c in s int(c) int c nums = list(reversed([int(c) for c in s])) + [0] nums list(reversed([int(c) for c in s])) + [0] list(reversed([int(c) for c in s])) list reversed([int(c) for c in s]) reversed [int(c) for c in s] [0] 0 r = (0, 1) r (0, 1) 0 1 for i in range(len(nums)):
    # print(r)
    next_no_borrow = min(r[0] + nums[i], r[1] + nums[i] + 1)
    next_borrow = min(10 - nums[i] + r[0], 9 - nums[i] + r[1])
    r = (next_no_borrow, next_borrow) i range(len(nums)) range len(nums) len nums next_no_borrow = min(r[0] + nums[i], r[1] + nums[i] + 1) next_no_borrow min(r[0] + nums[i], r[1] + nums[i] + 1) min r[0] + nums[i] r[0] r 0 nums[i] nums i r[1] + nums[i] + 1 r[1] + nums[i] r[1] r 1 nums[i] nums i 1 next_borrow = min(10 - nums[i] + r[0], 9 - nums[i] + r[1]) next_borrow min(10 - nums[i] + r[0], 9 - nums[i] + r[1]) min 10 - nums[i] + r[0] 10 - nums[i] 10 nums[i] nums i r[0] r 0 9 - nums[i] + r[1] 9 - nums[i] 9 nums[i] nums i r[1] r 1 r = (next_no_borrow, next_borrow) r (next_no_borrow, next_borrow) next_no_borrow next_borrow print(min(r)) print min(r) min r next_no_borrow = min(r[0] + nums[i], r[1] + nums[i] + 1) min(r[0] + nums[i], r[1] + nums[i] + 1) next_no_borrow r = (next_no_borrow, next_borrow) (next_no_borrow, next_borrow) r nums = list(reversed([int(c) for c in s])) + [0] list(reversed([int(c) for c in s])) + [0] nums next_borrow = min(10 - nums[i] + r[0], 9 - nums[i] + r[1]) min(10 - nums[i] + r[0], 9 - nums[i] + r[1]) next_borrow r = (0, 1) (0, 1) r s = input() input() s
s = input()
nums = list(reversed([int(c) for c in s])) + [0]
r = (0, 1) # no borrow, with borrow
for i in range(len(nums)):
    # print(r)
    next_no_borrow = min(r[0] + nums[i], r[1] + nums[i] + 1)
    next_borrow = min(10 - nums[i] + r[0], 9 - nums[i] + r[1])
    r = (next_no_borrow, next_borrow)
print(min(r))
