s = input(); n = len(s)
dp = [ [0,1<<32] for i in range(2) ]
for i in range(n):
  d = ord(s[i])-ord('0')
  dp[(i+1)%2][0] = min(dp[i%2][0], dp[i%2][1])+d
  dp[(i+1)%2][1] = min(dp[i%2][0], dp[i%2][1]-2)+11-d
print(min(dp[n%2][0],dp[n%2][1])) s = input() s input() input n = len(s) n len(s) len s [0,1<<32] for i in range(2) for i in range(2) i range(2) range 2 for i in range(2) [0,1<<32] 0 1<<32 1 32 dp = [ [0,1<<32] for i in range(2) ] dp [ [0,1<<32] for i in range(2) ] for i in range(n):
  d = ord(s[i])-ord('0')
  dp[(i+1)%2][0] = min(dp[i%2][0], dp[i%2][1])+d
  dp[(i+1)%2][1] = min(dp[i%2][0], dp[i%2][1]-2)+11-d i range(n) range n d = ord(s[i])-ord('0') d ord(s[i])-ord('0') ord(s[i]) ord s[i] s i ord('0') ord '0' dp[(i+1)%2][0] = min(dp[i%2][0], dp[i%2][1])+d dp[(i+1)%2][0] [(i+1)%2] dp (i+1)%2 i+1 i 1 2 0 min(dp[i%2][0], dp[i%2][1])+d min(dp[i%2][0], dp[i%2][1]) min dp[i%2][0] [i%2] dp i%2 i 2 0 dp[i%2][1] [i%2] dp i%2 i 2 1 d dp[(i+1)%2][1] = min(dp[i%2][0], dp[i%2][1]-2)+11-d dp[(i+1)%2][1] [(i+1)%2] dp (i+1)%2 i+1 i 1 2 1 min(dp[i%2][0], dp[i%2][1]-2)+11-d min(dp[i%2][0], dp[i%2][1]-2)+11 min(dp[i%2][0], dp[i%2][1]-2) min dp[i%2][0] [i%2] dp i%2 i 2 0 dp[i%2][1]-2 dp[i%2][1] [i%2] dp i%2 i 2 1 2 11 d print(min(dp[n%2][0],dp[n%2][1])) print min(dp[n%2][0],dp[n%2][1]) min dp[n%2][0] [n%2] dp n%2 n 2 0 dp[n%2][1] [n%2] dp n%2 n 2 1 n = len(s) len(s) n dp = [ [0,1<<32] for i in range(2) ] [ [0,1<<32] for i in range(2) ] dp d = ord(s[i])-ord('0') ord(s[i])-ord('0') d s = input() input() s
s = input(); n = len(s)
dp = [ [0,1<<32] for i in range(2) ]
for i in range(n):
  d = ord(s[i])-ord('0')
  dp[(i+1)%2][0] = min(dp[i%2][0], dp[i%2][1])+d
  dp[(i+1)%2][1] = min(dp[i%2][0], dp[i%2][1]-2)+11-d
print(min(dp[n%2][0],dp[n%2][1]))