s='0'

s+=eval(input())

n=len(s)

inf=float('inf')

dp=[[inf]*2 for _ in range(n+1)]

dp[0][0]=0

for i in range(n):

  for j in range(2):

    si=int(s[-i-1])

    si+=j

    for a in range(10):

      ni=i+1

      nj=0

      b=a-si

      if b<0:

        nj=1

        b+=10

      dp[ni][nj]=min(dp[ni][nj],dp[i][j]+a+b)

print((dp[n][0]))
s=eval(input())

n=len(s)

dp=0,1

for i in range(n):

  si=int(s[i])

  a=min(dp[0]+si,dp[1]+10-si)

  b=min(dp[0]+si+1,dp[1]+10-(si+1))

  dp=a,b

print((dp[0]))
s = "0"
s += eval(input())
n = len(s)
inf = float("inf")
dp = [[inf] * 2 for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
    for j in range(2):
        si = int(s[-i - 1])
        si += j
        for a in range(10):
            ni = i + 1
            nj = 0
            b = a - si
            if b < 0:
                nj = 1
                b += 10
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b)
print((dp[n][0]))

s = eval(input())
n = len(s)
dp = 0, 1
for i in range(n):
    si = int(s[i])
    a = min(dp[0] + si, dp[1] + 10 - si)
    b = min(dp[0] + si + 1, dp[1] + 10 - (si + 1))
    dp = a, b
print((dp[0]))

-s = "0" -s += eval(input()) +s = eval(input()) -inf = float("inf") -dp = [[inf] * 2 for _ in range(n + 1)] -dp[0][0] = 0 +dp = 0, 1 -    for j in range(2): -        si = int(s[-i - 1]) -        si += j -        for a in range(10): -            ni = i + 1 -            nj = 0 -            b = a - si -            if b < 0: -                nj = 1 -                b += 10 -            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b) -print((dp[n][0])) +    si = int(s[i]) +    a = min(dp[0] + si, dp[1] + 10 - si) +    b = min(dp[0] + si + 1, dp[1] + 10 - (si + 1)) +    dp = a, b +print((dp[0]))