N = ['0'] + list(eval(input())) + ['0']

# print (N)

n = len(N)

ans = 0

flag = False

count = 0

for i in range(n - 1, 0, -1):

    s = int(N[i])

    

    if flag:

        s += 1

        N[i] = str(s)

    else:

        pass



    if s <= 5:

        next_ = int(N[i - 1])

        if s == 5 and  next_ >= 5:

            # ans -= 1

            ans += s

            flag = True



        else:

            ans += s

            flag = False

    else:

        ans += 10 - s

        flag = True





if flag:

    ans += 1



count = 0

for s in N:

    if s == '5':

        count += 1

    else:

        ans -= max(count -2, 0)

        count = 0





print (ans)
S = ['0'] + list(eval(input())) + ['0']

N = len(S)

flag = False



ans = 0



for i in range(N - 1, 0, -1):

    j = int(S[i])

    if flag:

        j += 1

        S[i] = j



    if j <= 5:

        if j == 5 and int(S[i - 1]) >= 5:

            ans += j

            flag = True

        else:

            ans += j

            flag = False



    else:

        ans += 10 - j

        flag = True



if flag:

    ans += 1





# print (ans)

count = 0

for s in S:

    if s == 5:

        count += 1

    else:

        ans -= max(0, count - 2)

        count = 0



print (ans)

            





        

    

N = ["0"] + list(eval(input())) + ["0"]
# print (N)
n = len(N)
ans = 0
flag = False
count = 0
for i in range(n - 1, 0, -1):
    s = int(N[i])
    if flag:
        s += 1
        N[i] = str(s)
    else:
        pass
    if s <= 5:
        next_ = int(N[i - 1])
        if s == 5 and next_ >= 5:
            # ans -= 1
            ans += s
            flag = True
        else:
            ans += s
            flag = False
    else:
        ans += 10 - s
        flag = True
if flag:
    ans += 1
count = 0
for s in N:
    if s == "5":
        count += 1
    else:
        ans -= max(count - 2, 0)
        count = 0
print(ans)

S = ["0"] + list(eval(input())) + ["0"]
N = len(S)
flag = False
ans = 0
for i in range(N - 1, 0, -1):
    j = int(S[i])
    if flag:
        j += 1
        S[i] = j
    if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False
    else:
        ans += 10 - j
        flag = True
if flag:
    ans += 1
# print (ans)
count = 0
for s in S:
    if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0
print(ans)

-N = ["0"] + list(eval(input())) + ["0"] -# print (N) -n = len(N) +S = ["0"] + list(eval(input())) + ["0"] +N = len(S) +flag = False -flag = False -count = 0 -for i in range(n - 1, 0, -1): -    s = int(N[i]) +for i in range(N - 1, 0, -1): +    j = int(S[i]) -        s += 1 -        N[i] = str(s) -    else: -        pass -    if s <= 5: -        next_ = int(N[i - 1]) -        if s == 5 and next_ >= 5: -            # ans -= 1 -            ans += s +        j += 1 +        S[i] = j +    if j <= 5: +        if j == 5 and int(S[i - 1]) >= 5: +            ans += j -            ans += s +            ans += j -        ans += 10 - s +        ans += 10 - j +# print (ans) -for s in N: -    if s == "5": +for s in S: +    if s == 5: -        ans -= max(count - 2, 0) +        ans -= max(0, count - 2)