n = list(map(int, input()))
dp = 0,1
for i in n:
    a = min(dp[0]+i, dp[1]+10-i)
    b = min(dp[0]+i+1, dp[1]+10-(i+1))
    dp = a, b
print(dp[0]) n = list(map(int, input())) n list(map(int, input())) list map(int, input()) map int input() input dp = 0,1 dp dp = 0,1 0 1 for i in n:
    a = min(dp[0]+i, dp[1]+10-i)
    b = min(dp[0]+i+1, dp[1]+10-(i+1))
    dp = a, b i n a = min(dp[0]+i, dp[1]+10-i) a min(dp[0]+i, dp[1]+10-i) min dp[0]+i dp[0] dp 0 i dp[1]+10-i dp[1]+10 dp[1] dp 1 10 i b = min(dp[0]+i+1, dp[1]+10-(i+1)) b min(dp[0]+i+1, dp[1]+10-(i+1)) min dp[0]+i+1 dp[0]+i dp[0] dp 0 i 1 dp[1]+10-(i+1) dp[1]+10 dp[1] dp 1 10 i+1 i 1 dp = a, b dp dp = a, b a b print(dp[0]) print dp[0] dp 0 a = min(dp[0]+i, dp[1]+10-i) min(dp[0]+i, dp[1]+10-i) a n = list(map(int, input())) list(map(int, input())) n b = min(dp[0]+i+1, dp[1]+10-(i+1)) min(dp[0]+i+1, dp[1]+10-(i+1)) b dp = a, b dp = a, b dp dp = 0,1 dp = 0,1 dp
n = list(map(int, input()))
dp = 0,1
for i in n:
    a = min(dp[0]+i, dp[1]+10-i)
    b = min(dp[0]+i+1, dp[1]+10-(i+1))
    dp = a, b
print(dp[0])