N = list(input())
M = [ int(i) for i in N[::-1] ]
M.append(0)
c = 0

for i in range(len(M)-1):
    if M[i] >= 6 or ( M[i] >=5 and M[i+1] >= 5):
        M[i+1] += 1
    c += min(M[i],10-M[i])
c += M[len(M)-1]

print(c) N = list(input()) N list(input()) list input() input int(i) for i in N[::-1] for i in N[::-1] i N[::-1] N ::-1 -1 for i in N[::-1] int(i) int i M = [ int(i) for i in N[::-1] ] M [ int(i) for i in N[::-1] ] M.append(0) M.append M append 0 c = 0 c 0 for i in range(len(M)-1):
    if M[i] >= 6 or ( M[i] >=5 and M[i+1] >= 5):
        M[i+1] += 1
    c += min(M[i],10-M[i]) i range(len(M)-1) range len(M)-1 len(M) len M 1 if M[i] >= 6 or ( M[i] >=5 and M[i+1] >= 5):
        M[i+1] += 1
     M[i] >= 6 or ( M[i] >=5 and M[i+1] >= 5) M[i] >= 6 M[i] M i 6 M[i] >=5 and M[i+1] >= 5 M[i] >=5 M[i] M i 5 M[i+1] >= 5 M[i+1] M i+1 i 1 5 M[i+1] += 1 M[i+1] M i+1 i 1 1 c += min(M[i],10-M[i]) c min(M[i],10-M[i]) min M[i] M i 10-M[i] 10 M[i] M i c += M[len(M)-1] c M[len(M)-1] M len(M)-1 len(M) len M 1 print(c) print c N = list(input()) list(input()) N c += M[len(M)-1] M[len(M)-1] c c += min(M[i],10-M[i]) min(M[i],10-M[i]) c M = [ int(i) for i in N[::-1] ] [ int(i) for i in N[::-1] ] M c = 0 0 c
N = list(input())
M = [ int(i) for i in N[::-1] ]
M.append(0)
c = 0

for i in range(len(M)-1):
    if M[i] >= 6 or ( M[i] >=5 and M[i+1] >= 5):
        M[i+1] += 1
    c += min(M[i],10-M[i])
c += M[len(M)-1]

print(c)