s = input()
n = len(s)

ans = 0
dp1 = [0]*(n+1)
dp0 = [0]*(n+1)

dp1[0] = 1

for i in range(n):
  k = int(s[i])
  if k < 5:
    dp0[i+1] = dp0[i] + k
  elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
  k += 1
  if k == 10:
    dp1[i+1] = dp1[i]
  elif k < 5:
    dp1[i+1] = dp0[i] + k
  elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k

print(dp0[n]) s = input() s input() input n = len(s) n len(s) len s ans = 0 ans 0 dp1 = [0]*(n+1) dp1 [0]*(n+1) [0] 0 n+1 n 1 dp0 = [0]*(n+1) dp0 [0]*(n+1) [0] 0 n+1 n 1 dp1[0] = 1 dp1[0] dp1 0 1 for i in range(n):
  k = int(s[i])
  if k < 5:
    dp0[i+1] = dp0[i] + k
  elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
  k += 1
  if k == 10:
    dp1[i+1] = dp1[i]
  elif k < 5:
    dp1[i+1] = dp0[i] + k
  elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k i range(n) range n k = int(s[i]) k int(s[i]) int s[i] s i if k < 5:
    dp0[i+1] = dp0[i] + k
  elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
   k < 5 k 5 dp0[i+1] = dp0[i] + k dp0[i+1] dp0 i+1 i 1 dp0[i] + k dp0[i] dp0 i k elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
   k > 5 k 5 dp0[i+1] = dp1[i] + 10-k dp0[i+1] dp0 i+1 i 1 dp1[i] + 10-k dp1[i] + 10 dp1[i] dp1 i 10 k if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
   i == 0 i 0 dp0[i+1] = dp0[i] + k dp0[i+1] dp0 i+1 i 1 dp0[i] + k dp0[i] dp0 i k elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
     int(s[i-1]) < 5 int(s[i-1]) int s[i-1] s i-1 i 1 5 dp0[i+1] = dp0[i] + k dp0[i+1] dp0 i+1 i 1 dp0[i] + k dp0[i] dp0 i k dp0[i+1] = dp1[i] + 10-k dp0[i+1] dp0 i+1 i 1 dp1[i] + 10-k dp1[i] + 10 dp1[i] dp1 i 10 k k += 1 k 1 if k == 10:
    dp1[i+1] = dp1[i]
  elif k < 5:
    dp1[i+1] = dp0[i] + k
  elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k k == 10 k 10 dp1[i+1] = dp1[i] dp1[i+1] dp1 i+1 i 1 dp1[i] dp1 i elif k < 5:
    dp1[i+1] = dp0[i] + k
   k < 5 k 5 dp1[i+1] = dp0[i] + k dp1[i+1] dp1 i+1 i 1 dp0[i] + k dp0[i] dp0 i k elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
   k > 5 k 5 dp1[i+1] = dp1[i] + 10-k dp1[i+1] dp1 i+1 i 1 dp1[i] + 10-k dp1[i] + 10 dp1[i] dp1 i 10 k if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k i == 0 i 0 dp1[i+1] = dp0[i] + k dp1[i+1] dp1 i+1 i 1 dp0[i] + k dp0[i] dp0 i k elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
     int(s[i-1]) < 5 int(s[i-1]) int s[i-1] s i-1 i 1 5 dp1[i+1] = dp0[i] + k dp1[i+1] dp1 i+1 i 1 dp0[i] + k dp0[i] dp0 i k dp1[i+1] = dp1[i] + 10-k dp1[i+1] dp1 i+1 i 1 dp1[i] + 10-k dp1[i] + 10 dp1[i] dp1 i 10 k print(dp0[n]) print dp0[n] dp0 n n = len(s) len(s) n k = int(s[i]) int(s[i]) k k += 1 1 k dp0 = [0]*(n+1) [0]*(n+1) dp0 s = input() input() s dp1 = [0]*(n+1) [0]*(n+1) dp1 ans = 0 0 ans
s = input()
n = len(s)

ans = 0
dp1 = [0]*(n+1)
dp0 = [0]*(n+1)

dp1[0] = 1

for i in range(n):
  k = int(s[i])
  if k < 5:
    dp0[i+1] = dp0[i] + k
  elif k > 5:
    dp0[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp0[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp0[i+1] = dp0[i] + k
    else:
      dp0[i+1] = dp1[i] + 10-k
  k += 1
  if k == 10:
    dp1[i+1] = dp1[i]
  elif k < 5:
    dp1[i+1] = dp0[i] + k
  elif k > 5:
    dp1[i+1] = dp1[i] + 10-k
  else:
    if i == 0:
      dp1[i+1] = dp0[i] + k
    elif int(s[i-1]) < 5:
      dp1[i+1] = dp0[i] + k
    else:
      dp1[i+1] = dp1[i] + 10-k

print(dp0[n])