N = input()

dp0 = 0  
dp1 = 1  
 
for n in N:
    n = int(n)
    dp0_new = min(dp0 + n, dp1 + (10 - n))
    n = n + 1 #繰り上げさせてる
    dp1_new = min(dp0 + n, dp1 + (10 - n))
    dp0, dp1 = dp0_new, dp1_new

print(dp0) N = input() N input() input dp0 = 0 dp0 0 dp1 = 1 dp1 1 for n in N:
    n = int(n)
    dp0_new = min(dp0 + n, dp1 + (10 - n))
    n = n + 1 #繰り上げさせてる
    dp1_new = min(dp0 + n, dp1 + (10 - n))
    dp0, dp1 = dp0_new, dp1_new n N n = int(n) n int(n) int n dp0_new = min(dp0 + n, dp1 + (10 - n)) dp0_new min(dp0 + n, dp1 + (10 - n)) min dp0 + n dp0 n dp1 + (10 - n) dp1 10 - n 10 n n = n + 1 n n + 1 n 1 dp1_new = min(dp0 + n, dp1 + (10 - n)) dp1_new min(dp0 + n, dp1 + (10 - n)) min dp0 + n dp0 n dp1 + (10 - n) dp1 10 - n 10 n dp0, dp1 = dp0_new, dp1_new dp0 dp0_new dp1 dp1_new print(dp0) print dp0 dp1 = 1 1 dp1 dp1_new = min(dp0 + n, dp1 + (10 - n)) min(dp0 + n, dp1 + (10 - n)) dp1_new n = int(n) int(n) n N = input() input() N n = n + 1 n + 1 n dp0_new = min(dp0 + n, dp1 + (10 - n)) min(dp0 + n, dp1 + (10 - n)) dp0_new dp1 = dp0_new, dp1_new dp1_new dp1 dp0, dp1 = dp0_new, dp1_new dp0_new dp0 dp0 = 0 0 dp0
N = input()

dp0 = 0  
dp1 = 1  
 
for n in N:
    n = int(n)
    dp0_new = min(dp0 + n, dp1 + (10 - n))
    n = n + 1 #繰り上げさせてる
    dp1_new = min(dp0 + n, dp1 + (10 - n))
    dp0, dp1 = dp0_new, dp1_new

print(dp0)