N = input()

keta = len(N)

dp = [[10**18 for i in range(2)] for j in range(keta)]

t = int(N[-1])

dp[0][0] = t
dp[0][1] = 10 - t

for i in range(keta - 2, -1, -1):
    
    t = int(N[i])
    #print(keta-i,i,t)
    dp[keta-i-1][0] = min(dp[keta-i-2][0]+t,dp[keta-i-2][1]+t+1)
    dp[keta-i-1][1] = min(dp[keta-i-2][0]+(10-t),dp[keta-i-2][1]+(10-(t+1)))
    #a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))

#print(dp)

print(min(dp[keta-1][0],dp[keta-1][1]+1)) N = input() N input() input keta = len(N) keta len(N) len N [10**18 for i in range(2)] for j in range(keta) for j in range(keta) j range(keta) range keta for j in range(keta) [10**18 for i in range(2)] dp = [[10**18 for i in range(2)] for j in range(keta)] dp [[10**18 for i in range(2)] for j in range(keta)] t = int(N[-1]) t int(N[-1]) int N[-1] N -1 dp[0][0] = t dp[0][0] [0] dp 0 0 t dp[0][1] = 10 - t dp[0][1] [0] dp 0 1 10 - t 10 t for i in range(keta - 2, -1, -1):
    
    t = int(N[i])
    #print(keta-i,i,t)
    dp[keta-i-1][0] = min(dp[keta-i-2][0]+t,dp[keta-i-2][1]+t+1)
    dp[keta-i-1][1] = min(dp[keta-i-2][0]+(10-t),dp[keta-i-2][1]+(10-(t+1)))
    #a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))

#print(dp) i range(keta - 2, -1, -1) range keta - 2 keta 2 -1 -1 t = int(N[i]) t int(N[i]) int N[i] N i dp[keta-i-1][0] = min(dp[keta-i-2][0]+t,dp[keta-i-2][1]+t+1) dp[keta-i-1][0] [keta-i-1] dp keta-i-1 keta-i keta i 1 0 min(dp[keta-i-2][0]+t,dp[keta-i-2][1]+t+1) min dp[keta-i-2][0]+t dp[keta-i-2][0] [keta-i-2] dp keta-i-2 keta-i keta i 2 0 t dp[keta-i-2][1]+t+1 dp[keta-i-2][1]+t dp[keta-i-2][1] [keta-i-2] dp keta-i-2 keta-i keta i 2 1 t 1 dp[keta-i-1][1] = min(dp[keta-i-2][0]+(10-t),dp[keta-i-2][1]+(10-(t+1))) dp[keta-i-1][1] [keta-i-1] dp keta-i-1 keta-i keta i 1 1 min(dp[keta-i-2][0]+(10-t),dp[keta-i-2][1]+(10-(t+1))) min dp[keta-i-2][0]+(10-t) dp[keta-i-2][0] [keta-i-2] dp keta-i-2 keta-i keta i 2 0 10-t 10 t dp[keta-i-2][1]+(10-(t+1)) dp[keta-i-2][1] [keta-i-2] dp keta-i-2 keta-i keta i 2 1 10-(t+1) 10 t+1 t 1 print(min(dp[keta-1][0],dp[keta-1][1]+1)) print min(dp[keta-1][0],dp[keta-1][1]+1) min dp[keta-1][0] [keta-1] dp keta-1 keta 1 0 dp[keta-1][1]+1 dp[keta-1][1] [keta-1] dp keta-1 keta 1 1 1 keta = len(N) len(N) keta t = int(N[-1]) int(N[-1]) t dp = [[10**18 for i in range(2)] for j in range(keta)] [[10**18 for i in range(2)] for j in range(keta)] dp N = input() input() N t = int(N[i]) int(N[i]) t
N = input()

keta = len(N)

dp = [[10**18 for i in range(2)] for j in range(keta)]

t = int(N[-1])

dp[0][0] = t
dp[0][1] = 10 - t

for i in range(keta - 2, -1, -1):
    
    t = int(N[i])
    #print(keta-i,i,t)
    dp[keta-i-1][0] = min(dp[keta-i-2][0]+t,dp[keta-i-2][1]+t+1)
    dp[keta-i-1][1] = min(dp[keta-i-2][0]+(10-t),dp[keta-i-2][1]+(10-(t+1)))
    #a, b = min(a + t, b + (t + 1)), min(a + (10 - t), b + (10 - (t + 1)))

#print(dp)

print(min(dp[keta-1][0],dp[keta-1][1]+1))