n=input()
k=len(n)
dp=[[0]*2 for _ in range(k)]
dp[0][0]=min(int(n[0]),11-int(n[0]))
dp[0][1]=min(int(n[0])+1,11-(int(n[0])+1))
for i in range(1,k):
  dig=int(n[i])
  dp[i][0]=min(dp[i-1][0]+dig,dp[i-1][1]+10-dig)
  dp[i][1]=min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1))
print(dp[k-1][0]) n=input() n input() input k=len(n) k len(n) len n [0]*2 for _ in range(k) for _ in range(k) _ range(k) range k for _ in range(k) [0]*2 [0] 0 2 dp=[[0]*2 for _ in range(k)] dp [[0]*2 for _ in range(k)] dp[0][0]=min(int(n[0]),11-int(n[0])) dp[0][0] [0] dp 0 0 min(int(n[0]),11-int(n[0])) min int(n[0]) int n[0] n 0 11-int(n[0]) 11 int(n[0]) int n[0] n 0 dp[0][1]=min(int(n[0])+1,11-(int(n[0])+1)) dp[0][1] [0] dp 0 1 min(int(n[0])+1,11-(int(n[0])+1)) min int(n[0])+1 int(n[0]) int n[0] n 0 1 11-(int(n[0])+1) 11 int(n[0])+1 int(n[0]) int n[0] n 0 1 for i in range(1,k):
  dig=int(n[i])
  dp[i][0]=min(dp[i-1][0]+dig,dp[i-1][1]+10-dig)
  dp[i][1]=min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1)) i range(1,k) range 1 k dig=int(n[i]) dig int(n[i]) int n[i] n i dp[i][0]=min(dp[i-1][0]+dig,dp[i-1][1]+10-dig) dp[i][0] [i] dp i 0 min(dp[i-1][0]+dig,dp[i-1][1]+10-dig) min dp[i-1][0]+dig dp[i-1][0] [i-1] dp i-1 i 1 0 dig dp[i-1][1]+10-dig dp[i-1][1]+10 dp[i-1][1] [i-1] dp i-1 i 1 1 10 dig dp[i][1]=min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1)) dp[i][1] [i] dp i 1 min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1)) min dp[i-1][0]+dig+1 dp[i-1][0]+dig dp[i-1][0] [i-1] dp i-1 i 1 0 dig 1 dp[i-1][1]+10-(dig+1) dp[i-1][1]+10 dp[i-1][1] [i-1] dp i-1 i 1 1 10 dig+1 dig 1 print(dp[k-1][0]) print dp[k-1][0] [k-1] dp k-1 k 1 0 k=len(n) len(n) k dp=[[0]*2 for _ in range(k)] [[0]*2 for _ in range(k)] dp n=input() input() n dig=int(n[i]) int(n[i]) dig
n=input()
k=len(n)
dp=[[0]*2 for _ in range(k)]
dp[0][0]=min(int(n[0]),11-int(n[0]))
dp[0][1]=min(int(n[0])+1,11-(int(n[0])+1))
for i in range(1,k):
  dig=int(n[i])
  dp[i][0]=min(dp[i-1][0]+dig,dp[i-1][1]+10-dig)
  dp[i][1]=min(dp[i-1][0]+dig+1,dp[i-1][1]+10-(dig+1))
print(dp[k-1][0])