s = input()
dp = [[0, 0] for i in range(len(s))]
dp[0][0] = int(s[0])
dp[0][1] = 11 - int(s[0])
for i in range(1, len(s)):
  dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + int(s[i])
  dp[i][1] = min(dp[i - 1][0] + 11 - int(s[i]), dp[i - 1][1] + 9 - int(s[i]))
print(min(dp[-1][0], dp[-1][1])) s = input() s input() input [0, 0] for i in range(len(s)) for i in range(len(s)) i range(len(s)) range len(s) len s for i in range(len(s)) [0, 0] 0 0 dp = [[0, 0] for i in range(len(s))] dp [[0, 0] for i in range(len(s))] dp[0][0] = int(s[0]) dp[0][0] [0] dp 0 0 int(s[0]) int s[0] s 0 dp[0][1] = 11 - int(s[0]) dp[0][1] [0] dp 0 1 11 - int(s[0]) 11 int(s[0]) int s[0] s 0 for i in range(1, len(s)):
  dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + int(s[i])
  dp[i][1] = min(dp[i - 1][0] + 11 - int(s[i]), dp[i - 1][1] + 9 - int(s[i])) i range(1, len(s)) range 1 len(s) len s dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + int(s[i]) dp[i][0] [i] dp i 0 min(dp[i - 1][0], dp[i - 1][1]) + int(s[i]) min(dp[i - 1][0], dp[i - 1][1]) min dp[i - 1][0] [i - 1] dp i - 1 i 1 0 dp[i - 1][1] [i - 1] dp i - 1 i 1 1 int(s[i]) int s[i] s i dp[i][1] = min(dp[i - 1][0] + 11 - int(s[i]), dp[i - 1][1] + 9 - int(s[i])) dp[i][1] [i] dp i 1 min(dp[i - 1][0] + 11 - int(s[i]), dp[i - 1][1] + 9 - int(s[i])) min dp[i - 1][0] + 11 - int(s[i]) dp[i - 1][0] + 11 dp[i - 1][0] [i - 1] dp i - 1 i 1 0 11 int(s[i]) int s[i] s i dp[i - 1][1] + 9 - int(s[i]) dp[i - 1][1] + 9 dp[i - 1][1] [i - 1] dp i - 1 i 1 1 9 int(s[i]) int s[i] s i print(min(dp[-1][0], dp[-1][1])) print min(dp[-1][0], dp[-1][1]) min dp[-1][0] [-1] dp -1 0 dp[-1][1] [-1] dp -1 1 s = input() input() s dp = [[0, 0] for i in range(len(s))] [[0, 0] for i in range(len(s))] dp
s = input()
dp = [[0, 0] for i in range(len(s))]
dp[0][0] = int(s[0])
dp[0][1] = 11 - int(s[0])
for i in range(1, len(s)):
  dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + int(s[i])
  dp[i][1] = min(dp[i - 1][0] + 11 - int(s[i]), dp[i - 1][1] + 9 - int(s[i]))
print(min(dp[-1][0], dp[-1][1]))