#E DPでとく
N = [int(x) for x in input()]
dp = 0,1
for item in N:
    a = min(dp[0] + item, dp[1] + 10 - item)
    b = min(dp[0] + item + 1, dp[1] + 10 - (item + 1))
    dp = a, b
    
print(dp[0]) int(x) for x in input() for x in input() x input() input for x in input() int(x) int x N = [int(x) for x in input()] N [int(x) for x in input()] dp = 0,1 dp dp = 0,1 0 1 for item in N:
    a = min(dp[0] + item, dp[1] + 10 - item)
    b = min(dp[0] + item + 1, dp[1] + 10 - (item + 1))
    dp = a, b
     item N a = min(dp[0] + item, dp[1] + 10 - item) a min(dp[0] + item, dp[1] + 10 - item) min dp[0] + item dp[0] dp 0 item dp[1] + 10 - item dp[1] + 10 dp[1] dp 1 10 item b = min(dp[0] + item + 1, dp[1] + 10 - (item + 1)) b min(dp[0] + item + 1, dp[1] + 10 - (item + 1)) min dp[0] + item + 1 dp[0] + item dp[0] dp 0 item 1 dp[1] + 10 - (item + 1) dp[1] + 10 dp[1] dp 1 10 item + 1 item 1 dp = a, b dp dp = a, b a b print(dp[0]) print dp[0] dp 0 dp = 0,1 dp = 0,1 dp a = min(dp[0] + item, dp[1] + 10 - item) min(dp[0] + item, dp[1] + 10 - item) a b = min(dp[0] + item + 1, dp[1] + 10 - (item + 1)) min(dp[0] + item + 1, dp[1] + 10 - (item + 1)) b N = [int(x) for x in input()] [int(x) for x in input()] N dp = a, b dp = a, b dp
#E DPでとく
N = [int(x) for x in input()]
dp = 0,1
for item in N:
    a = min(dp[0] + item, dp[1] + 10 - item)
    b = min(dp[0] + item + 1, dp[1] + 10 - (item + 1))
    dp = a, b
    
print(dp[0])