#解説参照
#i桁目で10^i円多くはらうか否か

n_ =list(input())
n = [int(i) for i in n_]
ans = 0
if len(n_)==1:
    print(min(n[0],11-n[0]))
    exit()
dp1 = 0
dp2 = 1

for i in range(len(n)):
    dp_1 = min(dp1+n[i],dp2+10-n[i])
    dp_2 = min(dp1+n[i]+1,dp2+9-n[i])
    dp1 = dp_1
    dp2 = dp_2
print(dp1) n_ =list(input()) n_ list(input()) list input() input int(i) for i in n_ for i in n_ i n_ for i in n_ int(i) int i n = [int(i) for i in n_] n [int(i) for i in n_] ans = 0 ans 0 if len(n_)==1:
    print(min(n[0],11-n[0]))
    exit() len(n_)==1 len(n_) len n_ 1 print(min(n[0],11-n[0])) print min(n[0],11-n[0]) min n[0] n 0 11-n[0] 11 n[0] n 0 exit() exit dp1 = 0 dp1 0 dp2 = 1 dp2 1 for i in range(len(n)):
    dp_1 = min(dp1+n[i],dp2+10-n[i])
    dp_2 = min(dp1+n[i]+1,dp2+9-n[i])
    dp1 = dp_1
    dp2 = dp_2 i range(len(n)) range len(n) len n dp_1 = min(dp1+n[i],dp2+10-n[i]) dp_1 min(dp1+n[i],dp2+10-n[i]) min dp1+n[i] dp1 n[i] n i dp2+10-n[i] dp2+10 dp2 10 n[i] n i dp_2 = min(dp1+n[i]+1,dp2+9-n[i]) dp_2 min(dp1+n[i]+1,dp2+9-n[i]) min dp1+n[i]+1 dp1+n[i] dp1 n[i] n i 1 dp2+9-n[i] dp2+9 dp2 9 n[i] n i dp1 = dp_1 dp1 dp_1 dp2 = dp_2 dp2 dp_2 print(dp1) print dp1 dp_1 = min(dp1+n[i],dp2+10-n[i]) min(dp1+n[i],dp2+10-n[i]) dp_1 dp1 = dp_1 dp_1 dp1 dp2 = 1 1 dp2 n_ =list(input()) list(input()) n_ dp1 = 0 0 dp1 dp_2 = min(dp1+n[i]+1,dp2+9-n[i]) min(dp1+n[i]+1,dp2+9-n[i]) dp_2 n = [int(i) for i in n_] [int(i) for i in n_] n dp2 = dp_2 dp_2 dp2 ans = 0 0 ans
#解説参照
#i桁目で10^i円多くはらうか否か

n_ =list(input())
n = [int(i) for i in n_]
ans = 0
if len(n_)==1:
    print(min(n[0],11-n[0]))
    exit()
dp1 = 0
dp2 = 1

for i in range(len(n)):
    dp_1 = min(dp1+n[i],dp2+10-n[i])
    dp_2 = min(dp1+n[i]+1,dp2+9-n[i])
    dp1 = dp_1
    dp2 = dp_2
print(dp1)
