n = input()
a = int(n[-1])
b = 10 - int(n[-1])

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

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

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

print(min(a,b+1))