N = eval(input())

n = len(N)

a = int(N[0])

b = 11-int(N[0])

for i in range(n-1):

    a1 = min(a+int(N[i+1]), b+int(N[i+1]))

    b1 = min(a+11-int(N[i+1]), b+9-int(N[i+1]))

    a = a1

    b = b1

print((min(a,b)))
N = [int(s) for s in eval(input())]

n = len(N)

a = 0

b = 11

for n in N:

    a1 = min(a+n, b+n)

    b1 = min(a+11-n, b+9-n)

    a = a1

    b = b1

print((min(a,b)))
N = eval(input())
n = len(N)
a = int(N[0])
b = 11 - int(N[0])
for i in range(n - 1):
    a1 = min(a + int(N[i + 1]), b + int(N[i + 1]))
    b1 = min(a + 11 - int(N[i + 1]), b + 9 - int(N[i + 1]))
    a = a1
    b = b1
print((min(a, b)))

N = [int(s) for s in eval(input())]
n = len(N)
a = 0
b = 11
for n in N:
    a1 = min(a + n, b + n)
    b1 = min(a + 11 - n, b + 9 - n)
    a = a1
    b = b1
print((min(a, b)))

-N = eval(input()) +N = [int(s) for s in eval(input())] -a = int(N[0]) -b = 11 - int(N[0]) -for i in range(n - 1): -    a1 = min(a + int(N[i + 1]), b + int(N[i + 1])) -    b1 = min(a + 11 - int(N[i + 1]), b + 9 - int(N[i + 1])) +a = 0 +b = 11 +for n in N: +    a1 = min(a + n, b + n) +    b1 = min(a + 11 - n, b + 9 - n)