N = input()[::-1]
l = len(N)

n0 = int(N[0])
dp = [[0, 0] for i in range(l)]
dp[0] = [n0, 11-n0]

for i, n in enumerate(N[1:], 1):
  n = int(n)
  dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n)
  dp[i][1] = min(dp[i-1][0]+1+(10-n), dp[i-1][1]+(9-n))


print(min(dp[-1])) N = input()[::-1] N input()[::-1] () input ::-1 -1 l = len(N) l len(N) len N n0 = int(N[0]) n0 int(N[0]) int N[0] N 0 [0, 0] for i in range(l) for i in range(l) i range(l) range l for i in range(l) [0, 0] 0 0 dp = [[0, 0] for i in range(l)] dp [[0, 0] for i in range(l)] dp[0] = [n0, 11-n0] dp[0] dp 0 [n0, 11-n0] n0 11-n0 11 n0 for i, n in enumerate(N[1:], 1):
  n = int(n)
  dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n)
  dp[i][1] = min(dp[i-1][0]+1+(10-n), dp[i-1][1]+(9-n)) i n enumerate(N[1:], 1) enumerate N[1:] N 1: 1 1 n = int(n) n int(n) int n dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n) dp[i][0] [i] dp i 0 min(dp[i-1][0]+n, dp[i-1][1]+n) min dp[i-1][0]+n dp[i-1][0] [i-1] dp i-1 i 1 0 n dp[i-1][1]+n dp[i-1][1] [i-1] dp i-1 i 1 1 n dp[i][1] = min(dp[i-1][0]+1+(10-n), dp[i-1][1]+(9-n)) dp[i][1] [i] dp i 1 min(dp[i-1][0]+1+(10-n), dp[i-1][1]+(9-n)) min dp[i-1][0]+1+(10-n) dp[i-1][0]+1 dp[i-1][0] [i-1] dp i-1 i 1 0 1 10-n 10 n dp[i-1][1]+(9-n) dp[i-1][1] [i-1] dp i-1 i 1 1 9-n 9 n print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 l = len(N) len(N) l n = int(n) int(n) n n0 = int(N[0]) int(N[0]) n0 N = input()[::-1] input()[::-1] N dp = [[0, 0] for i in range(l)] [[0, 0] for i in range(l)] dp
N = input()[::-1]
l = len(N)

n0 = int(N[0])
dp = [[0, 0] for i in range(l)]
dp[0] = [n0, 11-n0]

for i, n in enumerate(N[1:], 1):
  n = int(n)
  dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n)
  dp[i][1] = min(dp[i-1][0]+1+(10-n), dp[i-1][1]+(9-n))


print(min(dp[-1]))