N = input()

a = 0  # not borrowed
b = 0  # borrowed

t = int(N[-1])
a, b = a + t, a + (10 - t)

for i in range(len(N) - 2, -1, -1):
    t = int(N[i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
print(min(a, b + 1)) N = input() N input() input a = 0 a 0 b = 0 b 0 t = int(N[-1]) t int(N[-1]) int N[-1] N -1 a, b = a + t, a + (10 - t) a a + t a t b a + (10 - t) a 10 - t 10 t for i in range(len(N) - 2, -1, -1):
    t = int(N[i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1))) i range(len(N) - 2, -1, -1) range len(N) - 2 len(N) len N 2 -1 -1 t = int(N[i]) t int(N[i]) int N[i] N i a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1))) a min(a + t, b + (t + 1)) min a + t a t b + (t + 1) b t + 1 t 1 b min(a + (10 - t), b + (10 - (t + 1))) min a + (10 - t) a 10 - t 10 t b + (10 - (t + 1)) b 10 - (t + 1) 10 t + 1 t 1 print(min(a, b + 1)) print min(a, b + 1) min a b + 1 b 1 a = 0 0 a t = int(N[i]) int(N[i]) t t = int(N[-1]) int(N[-1]) t N = input() input() N b = 0 0 b b = a + t, a + (10 - t) a + (10 - t) b a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1))) min(a + t, b + (t + 1)) a b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1))) min(a + (10 - t), b + (10 - (t + 1))) b a, b = a + t, a + (10 - t) a + t a
N = input()

a = 0  # not borrowed
b = 0  # borrowed

t = int(N[-1])
a, b = a + t, a + (10 - t)

for i in range(len(N) - 2, -1, -1):
    t = int(N[i])
    a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))
print(min(a, b + 1))
