s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])
 s = input() s input() input l = len(s) l len(s) len s INF = float('inf') INF float('inf') float 'inf' [INF]*2 for _ in range(l+1) for _ in range(l+1) _ range(l+1) range l+1 l 1 for _ in range(l+1) [INF]*2 [INF] INF 2 dp = [[INF]*2 for _ in range(l+1)] dp [[INF]*2 for _ in range(l+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(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1) i range(l) range l num = int(s[i]) num int(s[i]) int s[i] s i dp0 = dp[i][0] dp0 dp[i][0] [i] dp i 0 dp1 = dp[i][1] dp1 dp[i][1] [i] dp i 1 dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1)) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp0+num, dp1+min(10-num,num+1)) min dp0+num dp0 num dp1+min(10-num,num+1) dp1 min(10-num,num+1) min 10-num 10 num num+1 num 1 dp[i+1][1] = min(dp0+num+1,dp1+10-num-1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp0+num+1,dp1+10-num-1) min dp0+num+1 dp0+num dp0 num 1 dp1+10-num-1 dp1+10-num dp1+10 dp1 10 num 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 INF = float('inf') float('inf') INF dp = [[INF]*2 for _ in range(l+1)] [[INF]*2 for _ in range(l+1)] dp num = int(s[i]) int(s[i]) num dp0 = dp[i][0] dp[i][0] dp0 s = input() input() s dp1 = dp[i][1] dp[i][1] dp1 l = len(s) len(s) l
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

