s = list(list(map(int,input())))
n = len(s)
dp = [ [0,0] for i in range(n) ]
dp[0][0] = s[0]
dp[0][1] = 11-(s[0]-0)
for i in range(n-1):
  d = s[i+1]
  dp[i+1][0] = min(dp[i][0], dp[i][1]) + d
  dp[i+1][1] = min(dp[i][0]+11-d, dp[i][1]+9-d)
print(min(dp[n-1][0],dp[n-1][1])) s = list(list(map(int,input()))) s list(list(map(int,input()))) list list(map(int,input())) list map(int,input()) map int input() input n = len(s) n len(s) len s [0,0] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0,0] 0 0 dp = [ [0,0] for i in range(n) ] dp [ [0,0] for i in range(n) ] dp[0][0] = s[0] dp[0][0] [0] dp 0 0 s[0] s 0 dp[0][1] = 11-(s[0]-0) dp[0][1] [0] dp 0 1 11-(s[0]-0) 11 s[0]-0 s[0] s 0 0 for i in range(n-1):
  d = s[i+1]
  dp[i+1][0] = min(dp[i][0], dp[i][1]) + d
  dp[i+1][1] = min(dp[i][0]+11-d, dp[i][1]+9-d) i range(n-1) range n-1 n 1 d = s[i+1] d s[i+1] s i+1 i 1 dp[i+1][0] = min(dp[i][0], dp[i][1]) + d dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0], dp[i][1]) + d min(dp[i][0], dp[i][1]) min dp[i][0] [i] dp i 0 dp[i][1] [i] dp i 1 d dp[i+1][1] = min(dp[i][0]+11-d, dp[i][1]+9-d) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+11-d, dp[i][1]+9-d) min dp[i][0]+11-d dp[i][0]+11 dp[i][0] [i] dp i 0 11 d dp[i][1]+9-d dp[i][1]+9 dp[i][1] [i] dp i 1 9 d print(min(dp[n-1][0],dp[n-1][1])) print min(dp[n-1][0],dp[n-1][1]) min dp[n-1][0] [n-1] dp n-1 n 1 0 dp[n-1][1] [n-1] dp n-1 n 1 1 dp = [ [0,0] for i in range(n) ] [ [0,0] for i in range(n) ] dp n = len(s) len(s) n s = list(list(map(int,input()))) list(list(map(int,input()))) s d = s[i+1] s[i+1] d
s = list(list(map(int,input())))
n = len(s)
dp = [ [0,0] for i in range(n) ]
dp[0][0] = s[0]
dp[0][1] = 11-(s[0]-0)
for i in range(n-1):
  d = s[i+1]
  dp[i+1][0] = min(dp[i][0], dp[i][1]) + d
  dp[i+1][1] = min(dp[i][0]+11-d, dp[i][1]+9-d)
print(min(dp[n-1][0],dp[n-1][1]))
