s = input()

INF = float('inf')
dp = [[INF,INF] for _ in range(len(s)+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(len(s)):
    dp[i+1][0] = min(dp[i][0]+int(s[i]), dp[i][1]+10-int(s[i]), dp[i][1]+int(s[i])+1)
    dp[i+1][1] = min(dp[i][0]+int(s[i])+1,dp[i][1]+10-int(s[i])-1)

print(dp[-1][0]) s = input() s input() input INF = float('inf') INF float('inf') float 'inf' [INF,INF] for _ in range(len(s)+1) for _ in range(len(s)+1) _ range(len(s)+1) range len(s)+1 len(s) len s 1 for _ in range(len(s)+1) [INF,INF] INF INF dp = [[INF,INF] for _ in range(len(s)+1)] dp [[INF,INF] for _ in range(len(s)+1)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 dp[0][1]=1 dp[0][1] [0] dp 0 1 1 for i in range(len(s)):
    dp[i+1][0] = min(dp[i][0]+int(s[i]), dp[i][1]+10-int(s[i]), dp[i][1]+int(s[i])+1)
    dp[i+1][1] = min(dp[i][0]+int(s[i])+1,dp[i][1]+10-int(s[i])-1) i range(len(s)) range len(s) len s dp[i+1][0] = min(dp[i][0]+int(s[i]), dp[i][1]+10-int(s[i]), dp[i][1]+int(s[i])+1) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0]+int(s[i]), dp[i][1]+10-int(s[i]), dp[i][1]+int(s[i])+1) min dp[i][0]+int(s[i]) dp[i][0] [i] dp i 0 int(s[i]) int s[i] s i dp[i][1]+10-int(s[i]) dp[i][1]+10 dp[i][1] [i] dp i 1 10 int(s[i]) int s[i] s i dp[i][1]+int(s[i])+1 dp[i][1]+int(s[i]) dp[i][1] [i] dp i 1 int(s[i]) int s[i] s i 1 dp[i+1][1] = min(dp[i][0]+int(s[i])+1,dp[i][1]+10-int(s[i])-1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+int(s[i])+1,dp[i][1]+10-int(s[i])-1) min dp[i][0]+int(s[i])+1 dp[i][0]+int(s[i]) dp[i][0] [i] dp i 0 int(s[i]) int s[i] s i 1 dp[i][1]+10-int(s[i])-1 dp[i][1]+10-int(s[i]) dp[i][1]+10 dp[i][1] [i] dp i 1 10 int(s[i]) int s[i] s i 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 s = input() input() s INF = float('inf') float('inf') INF dp = [[INF,INF] for _ in range(len(s)+1)] [[INF,INF] for _ in range(len(s)+1)] dp
s = input()

INF = float('inf')
dp = [[INF,INF] for _ in range(len(s)+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(len(s)):
    dp[i+1][0] = min(dp[i][0]+int(s[i]), dp[i][1]+10-int(s[i]), dp[i][1]+int(s[i])+1)
    dp[i+1][1] = min(dp[i][0]+int(s[i])+1,dp[i][1]+10-int(s[i])-1)

print(dp[-1][0])
