n = eval(input())

n = n[::-1]

l = len(n)

ans = 0

flg = 0

x = []

for j in range(l):

  i = n[j]

  i = int(i)

  if i+flg>=6:

    i += flg

    ans += (10-i)

    flg = 1

  elif i+flg == 5:

    if j == l-1:

      ans += i+flg

      flg = 0

    elif int(n[j+1]) >= 5:

      i += flg

      ans += (10-i)

      flg = 1

    else:

      ans += i+flg

      flg = 0

  else:

    i += flg

    ans += i

    flg = 0

if flg:

  ans += 1

print(ans)
n = eval(input())

n = n[::-1]

l = len(n)

ans = 0

flg = 0

x = []

for j in range(l):

  i = n[j]

  i = int(i)

  i += flg

  if i >= 6:

    ans += (10-i)

    flg = 1

  elif i == 5:

    if j == l-1:

      ans += i

      flg = 0

    elif int(n[j+1]) >= 5:

      ans += (10-i)

      flg = 1

    else:

      ans += i

      flg = 0

  else:

    ans += i

    flg = 0

if flg:

  ans += 1

print(ans)
n = eval(input())
n = n[::-1]
l = len(n)
ans = 0
flg = 0
x = []
for j in range(l):
    i = n[j]
    i = int(i)
    if i + flg >= 6:
        i += flg
        ans += 10 - i
        flg = 1
    elif i + flg == 5:
        if j == l - 1:
            ans += i + flg
            flg = 0
        elif int(n[j + 1]) >= 5:
            i += flg
            ans += 10 - i
            flg = 1
        else:
            ans += i + flg
            flg = 0
    else:
        i += flg
        ans += i
        flg = 0
if flg:
    ans += 1
print(ans)

n = eval(input())
n = n[::-1]
l = len(n)
ans = 0
flg = 0
x = []
for j in range(l):
    i = n[j]
    i = int(i)
    i += flg
    if i >= 6:
        ans += 10 - i
        flg = 1
    elif i == 5:
        if j == l - 1:
            ans += i
            flg = 0
        elif int(n[j + 1]) >= 5:
            ans += 10 - i
            flg = 1
        else:
            ans += i
            flg = 0
    else:
        ans += i
        flg = 0
if flg:
    ans += 1
print(ans)

-    if i + flg >= 6: -        i += flg +    i += flg +    if i >= 6: -    elif i + flg == 5: +    elif i == 5: -            ans += i + flg +            ans += i -            i += flg -            ans += i + flg +            ans += i -        i += flg