N = eval(input())



DP_just = [0]

DP_plus = [1]



for c in N:

    d = int(c)

    last_just = DP_just[-1]

    last_plus = DP_plus[-1]

    DP_just.append(min(last_just + d, last_plus + 10 - d))

    d += 1

    DP_plus.append(min(last_just + d, last_plus + 10 - d))



print((DP_just[-1]))
N = eval(input())



DP_just = 0

DP_plus = 1



for c in N:

    d = int(c)

    last_just = DP_just

    last_plus = DP_plus

    DP_just = min(last_just + d, last_plus + 10 - d)

    d += 1

    DP_plus = min(last_just + d, last_plus + 10 - d)



print(DP_just)
N = eval(input())
DP_just = [0]
DP_plus = [1]
for c in N:
    d = int(c)
    last_just = DP_just[-1]
    last_plus = DP_plus[-1]
    DP_just.append(min(last_just + d, last_plus + 10 - d))
    d += 1
    DP_plus.append(min(last_just + d, last_plus + 10 - d))
print((DP_just[-1]))

N = eval(input())
DP_just = 0
DP_plus = 1
for c in N:
    d = int(c)
    last_just = DP_just
    last_plus = DP_plus
    DP_just = min(last_just + d, last_plus + 10 - d)
    d += 1
    DP_plus = min(last_just + d, last_plus + 10 - d)
print(DP_just)

-DP_just = [0] -DP_plus = [1] +DP_just = 0 +DP_plus = 1 -    last_just = DP_just[-1] -    last_plus = DP_plus[-1] -    DP_just.append(min(last_just + d, last_plus + 10 - d)) +    last_just = DP_just +    last_plus = DP_plus +    DP_just = min(last_just + d, last_plus + 10 - d) -    DP_plus.append(min(last_just + d, last_plus + 10 - d)) -print((DP_just[-1])) +    DP_plus = min(last_just + d, last_plus + 10 - d) +print(DP_just)