# E - Payment

S = input()
N = len(S)

# 先頭からi桁目までの金額のやり取りに必要な紙幣の枚数
dp = [0]
dp_plus1 = [1]

for i in range(N):
    x = int(S[i])
    dp.append(min(dp[i] + x, dp_plus1[i] + (10 - x)))
    dp_plus1.append(min(dp[i] +  (x + 1), dp_plus1[i] + (10 -  (x + 1))))

print(dp[-1]) S = input() S input() input N = len(S) N len(S) len S dp = [0] dp [0] 0 dp_plus1 = [1] dp_plus1 [1] 1 for i in range(N):
    x = int(S[i])
    dp.append(min(dp[i] + x, dp_plus1[i] + (10 - x)))
    dp_plus1.append(min(dp[i] +  (x + 1), dp_plus1[i] + (10 -  (x + 1)))) i range(N) range N x = int(S[i]) x int(S[i]) int S[i] S i dp.append(min(dp[i] + x, dp_plus1[i] + (10 - x))) dp.append dp append min(dp[i] + x, dp_plus1[i] + (10 - x)) min dp[i] + x dp[i] dp i x dp_plus1[i] + (10 - x) dp_plus1[i] dp_plus1 i 10 - x 10 x dp_plus1.append(min(dp[i] +  (x + 1), dp_plus1[i] + (10 -  (x + 1)))) dp_plus1.append dp_plus1 append min(dp[i] +  (x + 1), dp_plus1[i] + (10 -  (x + 1))) min dp[i] +  (x + 1) dp[i] dp i x + 1 x 1 dp_plus1[i] + (10 -  (x + 1)) dp_plus1[i] dp_plus1 i 10 -  (x + 1) 10 x + 1 x 1 print(dp[-1]) print dp[-1] dp -1 N = len(S) len(S) N dp_plus1 = [1] [1] dp_plus1 S = input() input() S dp = [0] [0] dp x = int(S[i]) int(S[i]) x
# E - Payment

S = input()
N = len(S)

# 先頭からi桁目までの金額のやり取りに必要な紙幣の枚数
dp = [0]
dp_plus1 = [1]

for i in range(N):
    x = int(S[i])
    dp.append(min(dp[i] + x, dp_plus1[i] + (10 - x)))
    dp_plus1.append(min(dp[i] +  (x + 1), dp_plus1[i] + (10 -  (x + 1))))

print(dp[-1])