N = input()
dp = [[0]*2 for _ in range(1001000)]
dp[0][1] = 1
for i in range(len(N)):
    d = int(N[i])
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d)
    dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1)  # もらいすぎている状態を維持する
ans = dp[len(N)][0]
print(ans) N = input() N input() input [0]*2 for _ in range(1001000) for _ in range(1001000) _ range(1001000) range 1001000 for _ in range(1001000) [0]*2 [0] 0 2 dp = [[0]*2 for _ in range(1001000)] dp [[0]*2 for _ in range(1001000)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(len(N)):
    d = int(N[i])
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d)
    dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1)  # もらいすぎている状態を維持する i range(len(N)) range len(N) len N d = int(N[i]) d int(N[i]) int N[i] N i dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + d, dp[i][1] + 10-d) min dp[i][0] + d dp[i][0] [i] dp i 0 d dp[i][1] + 10-d dp[i][1] + 10 dp[i][1] [i] dp i 1 10 d dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + d + 1, dp[i][1] + 10-d-1) min dp[i][0] + d + 1 dp[i][0] + d dp[i][0] [i] dp i 0 d 1 dp[i][1] + 10-d-1 dp[i][1] + 10-d dp[i][1] + 10 dp[i][1] [i] dp i 1 10 d 1 ans = dp[len(N)][0] ans dp[len(N)][0] [len(N)] dp len(N) len N 0 print(ans) print ans ans = dp[len(N)][0] dp[len(N)][0] ans d = int(N[i]) int(N[i]) d dp = [[0]*2 for _ in range(1001000)] [[0]*2 for _ in range(1001000)] dp N = input() input() N
N = input()
dp = [[0]*2 for _ in range(1001000)]
dp[0][1] = 1
for i in range(len(N)):
    d = int(N[i])
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + 10-d)
    dp[i+1][1] = min(dp[i][0] + d + 1, dp[i][1] + 10-d-1)  # もらいすぎている状態を維持する
ans = dp[len(N)][0]
print(ans)
