N = input()
X = list(map(int, list(str(N))))

dp1 = [float('inf')] * (len(X) + 1)  # dp1[i] := 上からi番目の金額を丁度払う場合の最小値
dp2 = [float('inf')] * (len(X) + 1)  # dp2[i] := 上からi番目の金額を１周り大きい額で支払う場合の最小値
dp1[0] = 0
for i, x in enumerate(X):
    dp1[i + 1] = min(dp1[i] + x, dp2[i] + x)
    dp2[i + 1] = min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1)

print(min(dp1[-1], dp2[-1])) N = input() N input() input X = list(map(int, list(str(N)))) X list(map(int, list(str(N)))) list map(int, list(str(N))) map int list(str(N)) list str(N) str N dp1 = [float('inf')] * (len(X) + 1) dp1 [float('inf')] * (len(X) + 1) [float('inf')] float('inf') float 'inf' len(X) + 1 len(X) len X 1 dp2 = [float('inf')] * (len(X) + 1) dp2 [float('inf')] * (len(X) + 1) [float('inf')] float('inf') float 'inf' len(X) + 1 len(X) len X 1 dp1[0] = 0 dp1[0] dp1 0 0 for i, x in enumerate(X):
    dp1[i + 1] = min(dp1[i] + x, dp2[i] + x)
    dp2[i + 1] = min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1) i x enumerate(X) enumerate X dp1[i + 1] = min(dp1[i] + x, dp2[i] + x) dp1[i + 1] dp1 i + 1 i 1 min(dp1[i] + x, dp2[i] + x) min dp1[i] + x dp1[i] dp1 i x dp2[i] + x dp2[i] dp2 i x dp2[i + 1] = min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1) dp2[i + 1] dp2 i + 1 i 1 min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1) min dp1[i] + (10 - x) + 1 dp1[i] + (10 - x) dp1[i] dp1 i 10 - x 10 x 1 dp2[i] + (10 - x) - 1 dp2[i] + (10 - x) dp2[i] dp2 i 10 - x 10 x 1 print(min(dp1[-1], dp2[-1])) print min(dp1[-1], dp2[-1]) min dp1[-1] dp1 -1 dp2[-1] dp2 -1 dp2 = [float('inf')] * (len(X) + 1) [float('inf')] * (len(X) + 1) dp2 N = input() input() N X = list(map(int, list(str(N)))) list(map(int, list(str(N)))) X dp1 = [float('inf')] * (len(X) + 1) [float('inf')] * (len(X) + 1) dp1
N = input()
X = list(map(int, list(str(N))))

dp1 = [float('inf')] * (len(X) + 1)  # dp1[i] := 上からi番目の金額を丁度払う場合の最小値
dp2 = [float('inf')] * (len(X) + 1)  # dp2[i] := 上からi番目の金額を１周り大きい額で支払う場合の最小値
dp1[0] = 0
for i, x in enumerate(X):
    dp1[i + 1] = min(dp1[i] + x, dp2[i] + x)
    dp2[i + 1] = min(dp1[i] + (10 - x) + 1, dp2[i] + (10 - x) - 1)

print(min(dp1[-1], dp2[-1]))
