N = list(map(int, list(input())))[::-1]
L = len(N)

answer = 0
for i, n in enumerate(N):
    if n < 5:
        answer += n
    elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
    else:
        answer += 5
        if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1

print(answer) N = list(map(int, list(input())))[::-1] N list(map(int, list(input())))[::-1] (map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input ::-1 -1 L = len(N) L len(N) len N answer = 0 answer 0 for i, n in enumerate(N):
    if n < 5:
        answer += n
    elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
    else:
        answer += 5
        if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1 i n enumerate(N) enumerate N if n < 5:
        answer += n
    elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
    else:
        answer += 5
        if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1 n < 5 n 5 answer += n answer n elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
     n > 5 n 5 answer += 10 - n answer 10 - n 10 n if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
     i < L - 1 i L - 1 L 1 N[i + 1] += 1 N[i + 1] N i + 1 i 1 1 answer += 1 answer 1 answer += 5 answer 5 if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1 i < L - 1 i L - 1 L 1 if N[i + 1] >= 5:
                N[i + 1] += 1 N[i + 1] >= 5 N[i + 1] N i + 1 i 1 5 N[i + 1] += 1 N[i + 1] N i + 1 i 1 1 print(answer) print answer answer += 5 5 answer answer += n n answer N = list(map(int, list(input())))[::-1] list(map(int, list(input())))[::-1] N L = len(N) len(N) L answer += 1 1 answer answer = 0 0 answer answer += 10 - n 10 - n answer
N = list(map(int, list(input())))[::-1]
L = len(N)

answer = 0
for i, n in enumerate(N):
    if n < 5:
        answer += n
    elif n > 5:
        answer += 10 - n
        if i < L - 1:
            N[i + 1] += 1
        else:
            answer += 1
    else:
        answer += 5
        if i < L - 1:
            if N[i + 1] >= 5:
                N[i + 1] += 1

print(answer)