A = list(map(int, input()))
while A[-1] == 0:
  A.pop()
n = len(A)
dp = [[0]*2 for _ in range(n)]
dp[0][1] = 1
for i, a in enumerate(A[:-1]):
  if i < n-1:
    dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a)
    dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a)
ans = min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1])
print(ans) A = list(map(int, input())) A list(map(int, input())) list map(int, input()) map int input() input while A[-1] == 0:
  A.pop() A[-1] == 0 A[-1] A -1 0 A.pop() A.pop A pop n = len(A) n len(A) len A [0]*2 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0]*2 [0] 0 2 dp = [[0]*2 for _ in range(n)] dp [[0]*2 for _ in range(n)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i, a in enumerate(A[:-1]):
  if i < n-1:
    dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a)
    dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a) i a enumerate(A[:-1]) enumerate A[:-1] A :-1 -1 if i < n-1:
    dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a)
    dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a) i < n-1 i n-1 n 1 dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0]+a, dp[i][1]+10-a) min dp[i][0]+a dp[i][0] [i] dp i 0 a dp[i][1]+10-a dp[i][1]+10 dp[i][1] [i] dp i 1 10 a dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+a+1, dp[i][1]+9-a) min dp[i][0]+a+1 dp[i][0]+a dp[i][0] [i] dp i 0 a 1 dp[i][1]+9-a dp[i][1]+9 dp[i][1] [i] dp i 1 9 a ans = min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1]) ans min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1]) min dp[-1][0]+A[-1] dp[-1][0] [-1] dp -1 0 A[-1] A -1 dp[-1][1]+10-A[-1] dp[-1][1]+10 dp[-1][1] [-1] dp -1 1 10 A[-1] A -1 print(ans) print ans dp = [[0]*2 for _ in range(n)] [[0]*2 for _ in range(n)] dp n = len(A) len(A) n ans = min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1]) min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1]) ans A = list(map(int, input())) list(map(int, input())) A
A = list(map(int, input()))
while A[-1] == 0:
  A.pop()
n = len(A)
dp = [[0]*2 for _ in range(n)]
dp[0][1] = 1
for i, a in enumerate(A[:-1]):
  if i < n-1:
    dp[i+1][0] = min(dp[i][0]+a, dp[i][1]+10-a)
    dp[i+1][1] = min(dp[i][0]+a+1, dp[i][1]+9-a)
ans = min(dp[-1][0]+A[-1], dp[-1][1]+10-A[-1])
print(ans)