s = input()
s = "".join([ i for i in reversed(s)]) + "0"
 
INF = 10 ** 32
n = len(s)
dp = [ [INF]*2 for _ in range(n)]
 
dp[0][0] = 0
 
for i in range(n-1):
    x = int(s[i])
    dp[i+1][0] = min( dp[i][0]+x, dp[i][1]+x )
    dp[i+1][1] = min( dp[i][0]+1+(10-x), dp[i][1]+(9-x) )
 
print(min(dp[n-1][0], dp[n-1][1])) s = input() s input() input i for i in reversed(s) for i in reversed(s) i reversed(s) reversed s for i in reversed(s) i s = "".join([ i for i in reversed(s)]) + "0" s "".join([ i for i in reversed(s)]) + "0" "".join([ i for i in reversed(s)]) "".join "" join [ i for i in reversed(s)] "0" INF = 10 ** 32 INF 10 ** 32 10 32 n = len(s) n len(s) len s [INF]*2 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [INF]*2 [INF] INF 2 dp = [ [INF]*2 for _ in range(n)] dp [ [INF]*2 for _ in range(n)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(n-1):
    x = int(s[i])
    dp[i+1][0] = min( dp[i][0]+x, dp[i][1]+x )
    dp[i+1][1] = min( dp[i][0]+1+(10-x), dp[i][1]+(9-x) )
  i range(n-1) range n-1 n 1 x = int(s[i]) x int(s[i]) int s[i] s i dp[i+1][0] = min( dp[i][0]+x, dp[i][1]+x ) dp[i+1][0] [i+1] dp i+1 i 1 0 min( dp[i][0]+x, dp[i][1]+x ) min dp[i][0]+x dp[i][0] [i] dp i 0 x dp[i][1]+x dp[i][1] [i] dp i 1 x dp[i+1][1] = min( dp[i][0]+1+(10-x), dp[i][1]+(9-x) ) dp[i+1][1] [i+1] dp i+1 i 1 1 min( dp[i][0]+1+(10-x), dp[i][1]+(9-x) ) min dp[i][0]+1+(10-x) dp[i][0]+1 dp[i][0] [i] dp i 0 1 10-x 10 x dp[i][1]+(9-x) dp[i][1] [i] dp i 1 9-x 9 x print(min(dp[n-1][0], dp[n-1][1])) print min(dp[n-1][0], dp[n-1][1]) min dp[n-1][0] [n-1] dp n-1 n 1 0 dp[n-1][1] [n-1] dp n-1 n 1 1 x = int(s[i]) int(s[i]) x dp = [ [INF]*2 for _ in range(n)] [ [INF]*2 for _ in range(n)] dp n = len(s) len(s) n s = "".join([ i for i in reversed(s)]) + "0" "".join([ i for i in reversed(s)]) + "0" s INF = 10 ** 32 10 ** 32 INF s = input() input() s
s = input()
s = "".join([ i for i in reversed(s)]) + "0"
 
INF = 10 ** 32
n = len(s)
dp = [ [INF]*2 for _ in range(n)]
 
dp[0][0] = 0
 
for i in range(n-1):
    x = int(s[i])
    dp[i+1][0] = min( dp[i][0]+x, dp[i][1]+x )
    dp[i+1][1] = min( dp[i][0]+1+(10-x), dp[i][1]+(9-x) )
 
print(min(dp[n-1][0], dp[n-1][1]))