N = [int(i) for i in input()]

dp = [0, float('inf')]
# dp = [桁上げしてない, 桁上げあった]
for n in N :
  ndp = [0] * 2
  ndp[0] = min(dp[0] + n, dp[1] + n)
  ndp[1] = min(dp[0] + 11 - n, dp[1] + 9 - n)
  dp = ndp

print(min(dp[0], dp[1])) int(i) for i in input() for i in input() i input() input for i in input() int(i) int i N = [int(i) for i in input()] N [int(i) for i in input()] dp = [0, float('inf')] dp [0, float('inf')] 0 float('inf') float 'inf' for n in N :
  ndp = [0] * 2
  ndp[0] = min(dp[0] + n, dp[1] + n)
  ndp[1] = min(dp[0] + 11 - n, dp[1] + 9 - n)
  dp = ndp n N ndp = [0] * 2 ndp [0] * 2 [0] 0 2 ndp[0] = min(dp[0] + n, dp[1] + n) ndp[0] ndp 0 min(dp[0] + n, dp[1] + n) min dp[0] + n dp[0] dp 0 n dp[1] + n dp[1] dp 1 n ndp[1] = min(dp[0] + 11 - n, dp[1] + 9 - n) ndp[1] ndp 1 min(dp[0] + 11 - n, dp[1] + 9 - n) min dp[0] + 11 - n dp[0] + 11 dp[0] dp 0 11 n dp[1] + 9 - n dp[1] + 9 dp[1] dp 1 9 n dp = ndp dp ndp print(min(dp[0], dp[1])) print min(dp[0], dp[1]) min dp[0] dp 0 dp[1] dp 1 ndp = [0] * 2 [0] * 2 ndp N = [int(i) for i in input()] [int(i) for i in input()] N dp = ndp ndp dp dp = [0, float('inf')] [0, float('inf')] dp
N = [int(i) for i in input()]

dp = [0, float('inf')]
# dp = [桁上げしてない, 桁上げあった]
for n in N :
  ndp = [0] * 2
  ndp[0] = min(dp[0] + n, dp[1] + n)
  ndp[1] = min(dp[0] + 11 - n, dp[1] + 9 - n)
  dp = ndp

print(min(dp[0], dp[1]))