N = input()
a, b = 0, 1
for i in N:
    x = int(i)
    a, b = min(a+x, b+10-x), min(a+x+1, b+9-x)
print (a) N = input() N input() input a, b = 0, 1 a 0 b 1 for i in N:
    x = int(i)
    a, b = min(a+x, b+10-x), min(a+x+1, b+9-x) i N x = int(i) x int(i) int i a, b = min(a+x, b+10-x), min(a+x+1, b+9-x) a min(a+x, b+10-x) min a+x a x b+10-x b+10 b 10 x b min(a+x+1, b+9-x) min a+x+1 a+x a x 1 b+9-x b+9 b 9 x print (a) print a a, b = min(a+x, b+10-x), min(a+x+1, b+9-x) min(a+x, b+10-x) a a, b = 0, 1 0 a N = input() input() N x = int(i) int(i) x b = min(a+x, b+10-x), min(a+x+1, b+9-x) min(a+x+1, b+9-x) b b = 0, 1 1 b
N = input()
a, b = 0, 1
for i in N:
    x = int(i)
    a, b = min(a+x, b+10-x), min(a+x+1, b+9-x)
print (a)