n=[int(x) for x in input()]

dp=[[100000000000 for i in range(len(n)+1)]for j in range(2)]
dp[0][0]=0
dp[1][0]=1
for i in range(len(n)):
  d=n[i]
  dp[0][i+1]=min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i])
  dp[1][i+1]=min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i])
  
  
print(dp[0][len(n)])
#print(dp) int(x) for x in input() for x in input() x input() input for x in input() int(x) int x n=[int(x) for x in input()] n [int(x) for x in input()] [100000000000 for i in range(len(n)+1)]for j in range(2) for j in range(2) j range(2) range 2 for j in range(2) [100000000000 for i in range(len(n)+1)] dp=[[100000000000 for i in range(len(n)+1)]for j in range(2)] dp [[100000000000 for i in range(len(n)+1)]for j in range(2)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 dp[1][0]=1 dp[1][0] [1] dp 1 0 1 for i in range(len(n)):
  d=n[i]
  dp[0][i+1]=min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i])
  dp[1][i+1]=min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i])
  
   i range(len(n)) range len(n) len n d=n[i] d n[i] n i dp[0][i+1]=min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i]) dp[0][i+1] [0] dp 0 i+1 i 1 min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i]) min dp[0][i+1] [0] dp 0 i+1 i 1 min(d,11-d)+dp[0][i] min(d,11-d) min d 11-d 11 d dp[0][i] [0] dp 0 i 10-d+dp[1][i] 10-d 10 d dp[1][i] [1] dp 1 i dp[1][i+1]=min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i]) dp[1][i+1] [1] dp 1 i+1 i 1 min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i]) min dp[1][i+1] [1] dp 1 i+1 i 1 d+1+dp[0][i] d+1 d 1 dp[0][i] [0] dp 0 i 9-d+dp[1][i] 9-d 9 d dp[1][i] [1] dp 1 i print(dp[0][len(n)]) print dp[0][len(n)] [0] dp 0 len(n) len n dp=[[100000000000 for i in range(len(n)+1)]for j in range(2)] [[100000000000 for i in range(len(n)+1)]for j in range(2)] dp d=n[i] n[i] d n=[int(x) for x in input()] [int(x) for x in input()] n
n=[int(x) for x in input()]

dp=[[100000000000 for i in range(len(n)+1)]for j in range(2)]
dp[0][0]=0
dp[1][0]=1
for i in range(len(n)):
  d=n[i]
  dp[0][i+1]=min(dp[0][i+1],min(d,11-d)+dp[0][i],10-d+dp[1][i])
  dp[1][i+1]=min(dp[1][i+1],d+1+dp[0][i],9-d+dp[1][i])
  
  
print(dp[0][len(n)])
#print(dp)