N=input()
inf=float("inf")
dp=[[inf]*(len(N)+1) for i in range(2)]
dp[0][0]=0
for i in range(len(N)):
   tmp=int(N[-1-i])
   dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp
   dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp
print(min(dp[0][-1],dp[1][-1])) N=input() N input() input inf=float("inf") inf float("inf") float "inf" [inf]*(len(N)+1) for i in range(2) for i in range(2) i range(2) range 2 for i in range(2) [inf]*(len(N)+1) [inf] inf len(N)+1 len(N) len N 1 dp=[[inf]*(len(N)+1) for i in range(2)] dp [[inf]*(len(N)+1) for i in range(2)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 for i in range(len(N)):
   tmp=int(N[-1-i])
   dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp
   dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp i range(len(N)) range len(N) len N tmp=int(N[-1-i]) tmp int(N[-1-i]) int N[-1-i] N -1-i -1 i dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp dp[0][i+1] [0] dp 0 i+1 i 1 min(dp[0][i],dp[1][i])+tmp min(dp[0][i],dp[1][i]) min dp[0][i] [0] dp 0 i dp[1][i] [1] dp 1 i tmp dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp dp[1][i+1] [1] dp 1 i+1 i 1 min(dp[0][i]+11,dp[1][i]+9)-tmp min(dp[0][i]+11,dp[1][i]+9) min dp[0][i]+11 dp[0][i] [0] dp 0 i 11 dp[1][i]+9 dp[1][i] [1] dp 1 i 9 tmp print(min(dp[0][-1],dp[1][-1])) print min(dp[0][-1],dp[1][-1]) min dp[0][-1] [0] dp 0 -1 dp[1][-1] [1] dp 1 -1 inf=float("inf") float("inf") inf dp=[[inf]*(len(N)+1) for i in range(2)] [[inf]*(len(N)+1) for i in range(2)] dp N=input() input() N tmp=int(N[-1-i]) int(N[-1-i]) tmp
N=input()
inf=float("inf")
dp=[[inf]*(len(N)+1) for i in range(2)]
dp[0][0]=0
for i in range(len(N)):
   tmp=int(N[-1-i])
   dp[0][i+1]=min(dp[0][i],dp[1][i])+tmp
   dp[1][i+1]=min(dp[0][i]+11,dp[1][i]+9)-tmp
print(min(dp[0][-1],dp[1][-1]))