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