'''
提出#12888083
'''

L = list(map(int,list(input())))
L = [0] + L
N = len(L)
dp1 = [0 for _ in range(N+1)]
dp2 = [0 for _ in range(N+1)]

for i in range(N):
    dp1[i+1] = min(dp1[i] + L[i], dp2[i] + 10 - L[i])
    dp2[i+1] = min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1)

print(dp1[-1]) '''
提出#12888083
''' L = list(map(int,list(input()))) L list(map(int,list(input()))) list map(int,list(input())) map int list(input()) list input() input L = [0] + L L [0] + L [0] 0 L N = len(L) N len(L) len L 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 dp1 = [0 for _ in range(N+1)] dp1 [0 for _ in range(N+1)] 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 dp2 = [0 for _ in range(N+1)] dp2 [0 for _ in range(N+1)] for i in range(N):
    dp1[i+1] = min(dp1[i] + L[i], dp2[i] + 10 - L[i])
    dp2[i+1] = min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1) i range(N) range N dp1[i+1] = min(dp1[i] + L[i], dp2[i] + 10 - L[i]) dp1[i+1] dp1 i+1 i 1 min(dp1[i] + L[i], dp2[i] + 10 - L[i]) min dp1[i] + L[i] dp1[i] dp1 i L[i] L i dp2[i] + 10 - L[i] dp2[i] + 10 dp2[i] dp2 i 10 L[i] L i dp2[i+1] = min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1) dp2[i+1] dp2 i+1 i 1 min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1) min dp2[i] + 10 - L[i] - 1 dp2[i] + 10 - L[i] dp2[i] + 10 dp2[i] dp2 i 10 L[i] L i 1 dp1[i] + L[i] + 1 dp1[i] + L[i] dp1[i] dp1 i L[i] L i 1 print(dp1[-1]) print dp1[-1] dp1 -1 dp2 = [0 for _ in range(N+1)] [0 for _ in range(N+1)] dp2 N = len(L) len(L) N L = [0] + L [0] + L L L = list(map(int,list(input()))) list(map(int,list(input()))) L dp1 = [0 for _ in range(N+1)] [0 for _ in range(N+1)] dp1
'''
提出#12888083
'''

L = list(map(int,list(input())))
L = [0] + L
N = len(L)
dp1 = [0 for _ in range(N+1)]
dp2 = [0 for _ in range(N+1)]

for i in range(N):
    dp1[i+1] = min(dp1[i] + L[i], dp2[i] + 10 - L[i])
    dp2[i+1] = min(dp2[i] + 10 - L[i] - 1, dp1[i] + L[i] + 1)

print(dp1[-1])
