


n = list(map(int, list(input())))[::-1] + [0]
sum1 = sum(n)
for i in range(len(n)-1):
    if n[i] > 5 or (n[i]==5 and n[i+1]>4):
        n[i] = 10-n[i]
        n[i+1] += 1
print(min(sum1, sum(n))) n = list(map(int, list(input())))[::-1] + [0] n list(map(int, list(input())))[::-1] + [0] list(map(int, list(input())))[::-1] (map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input ::-1 -1 [0] 0 sum1 = sum(n) sum1 sum(n) sum n for i in range(len(n)-1):
    if n[i] > 5 or (n[i]==5 and n[i+1]>4):
        n[i] = 10-n[i]
        n[i+1] += 1 i range(len(n)-1) range len(n)-1 len(n) len n 1 if n[i] > 5 or (n[i]==5 and n[i+1]>4):
        n[i] = 10-n[i]
        n[i+1] += 1 n[i] > 5 or (n[i]==5 and n[i+1]>4) n[i] > 5 n[i] n i 5 n[i]==5 and n[i+1]>4 n[i]==5 n[i] n i 5 n[i+1]>4 n[i+1] n i+1 i 1 4 n[i] = 10-n[i] n[i] n i 10-n[i] 10 n[i] n i n[i+1] += 1 n[i+1] n i+1 i 1 1 print(min(sum1, sum(n))) print min(sum1, sum(n)) min sum1 sum(n) sum n n = list(map(int, list(input())))[::-1] + [0] list(map(int, list(input())))[::-1] + [0] n sum1 = sum(n) sum(n) sum1



n = list(map(int, list(input())))[::-1] + [0]
sum1 = sum(n)
for i in range(len(n)-1):
    if n[i] > 5 or (n[i]==5 and n[i+1]>4):
        n[i] = 10-n[i]
        n[i+1] += 1
print(min(sum1, sum(n)))
