S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
dp[1][0]=dp[0][0]+int(S[0])
dp[1][1]=dp[0][1]+10-int(S[0])
for i in range(1,l):
  dp[i+1][0]=min(dp[i][0]+int(S[i]),dp[i][1]+int(S[i])+1)
  dp[i+1][1]=min(dp[i][0]+10-int(S[i]),dp[i][1]+10-int(S[i])-1)
  
print(min(dp[-1][0],dp[-1][1]+1)) S = input()[::-1] S input()[::-1] () input ::-1 -1 l = len(S) l len(S) len S [0]*2 for _ in range(l+1) for _ in range(l+1) _ range(l+1) range l+1 l 1 for _ in range(l+1) [0]*2 [0] 0 2 dp = [[0]*2 for _ in range(l+1)] dp [[0]*2 for _ in range(l+1)] dp[1][0]=dp[0][0]+int(S[0]) dp[1][0] [1] dp 1 0 dp[0][0]+int(S[0]) dp[0][0] [0] dp 0 0 int(S[0]) int S[0] S 0 dp[1][1]=dp[0][1]+10-int(S[0]) dp[1][1] [1] dp 1 1 dp[0][1]+10-int(S[0]) dp[0][1]+10 dp[0][1] [0] dp 0 1 10 int(S[0]) int S[0] S 0 for i in range(1,l):
  dp[i+1][0]=min(dp[i][0]+int(S[i]),dp[i][1]+int(S[i])+1)
  dp[i+1][1]=min(dp[i][0]+10-int(S[i]),dp[i][1]+10-int(S[i])-1)
   i range(1,l) range 1 l dp[i+1][0]=min(dp[i][0]+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]+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]+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]+10-int(S[i]),dp[i][1]+10-int(S[i])-1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+10-int(S[i]),dp[i][1]+10-int(S[i])-1) min dp[i][0]+10-int(S[i]) dp[i][0]+10 dp[i][0] [i] dp i 0 10 int(S[i]) int S[i] S i 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(min(dp[-1][0],dp[-1][1]+1)) print min(dp[-1][0],dp[-1][1]+1) min dp[-1][0] [-1] dp -1 0 dp[-1][1]+1 dp[-1][1] [-1] dp -1 1 1 dp = [[0]*2 for _ in range(l+1)] [[0]*2 for _ in range(l+1)] dp S = input()[::-1] input()[::-1] S l = len(S) len(S) l
S = input()[::-1]
l = len(S)
dp = [[0]*2 for _ in range(l+1)]
dp[1][0]=dp[0][0]+int(S[0])
dp[1][1]=dp[0][1]+10-int(S[0])
for i in range(1,l):
  dp[i+1][0]=min(dp[i][0]+int(S[i]),dp[i][1]+int(S[i])+1)
  dp[i+1][1]=min(dp[i][0]+10-int(S[i]),dp[i][1]+10-int(S[i])-1)
  
print(min(dp[-1][0],dp[-1][1]+1))