N = list(input())

num = 0
ans = 0
for i in range(len(N)-1, -1, -1):
    n = int(N[i])
    if n+num == 5 and i > 0 and int(N[i-1]) >= 5:
        ans += 5
        num = 1
    elif n+num < 6:
        ans += n+num
        num = 0
    else:
        ans += 10-(n+num)
        num = 1

ans += num
print(ans) N = list(input()) N list(input()) list input() input num = 0 num 0 ans = 0 ans 0 for i in range(len(N)-1, -1, -1):
    n = int(N[i])
    if n+num == 5 and i > 0 and int(N[i-1]) >= 5:
        ans += 5
        num = 1
    elif n+num < 6:
        ans += n+num
        num = 0
    else:
        ans += 10-(n+num)
        num = 1 i range(len(N)-1, -1, -1) range len(N)-1 len(N) len N 1 -1 -1 n = int(N[i]) n int(N[i]) int N[i] N i if n+num == 5 and i > 0 and int(N[i-1]) >= 5:
        ans += 5
        num = 1
    elif n+num < 6:
        ans += n+num
        num = 0
    else:
        ans += 10-(n+num)
        num = 1 n+num == 5 and i > 0 and int(N[i-1]) >= 5 n+num == 5 and i > 0 n+num == 5 n+num n num 5 i > 0 i 0 int(N[i-1]) >= 5 int(N[i-1]) int N[i-1] N i-1 i 1 5 ans += 5 ans 5 num = 1 num 1 elif n+num < 6:
        ans += n+num
        num = 0
     n+num < 6 n+num n num 6 ans += n+num ans n+num n num num = 0 num 0 ans += 10-(n+num) ans 10-(n+num) 10 n+num n num num = 1 num 1 ans += num ans num print(ans) print ans ans = 0 0 ans num = 1 1 num n = int(N[i]) int(N[i]) n ans += num num ans ans += 10-(n+num) 10-(n+num) ans num = 0 0 num N = list(input()) list(input()) N num = 1 1 num num = 0 0 num ans += 5 5 ans ans += n+num n+num ans
N = list(input())

num = 0
ans = 0
for i in range(len(N)-1, -1, -1):
    n = int(N[i])
    if n+num == 5 and i > 0 and int(N[i-1]) >= 5:
        ans += 5
        num = 1
    elif n+num < 6:
        ans += n+num
        num = 0
    else:
        ans += 10-(n+num)
        num = 1

ans += num
print(ans)