n = [0] + list(map(int, input())) + [0]
n = n[::-1]
#print(n)

b0 = [0]*len(n)
b1 = [0]*len(n)
b1[0] = 1
for i in range(1, len(n)):
  num = n[i]
  b0[i] = min(b0[i-1]+num, b1[i-1]+num+1)
  b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1))

print(min(b0[-1], b1[-1]))
#print(b0[-1], b1[-1]) n = [0] + list(map(int, input())) + [0] n [0] + list(map(int, input())) + [0] [0] + list(map(int, input())) [0] 0 list(map(int, input())) list map(int, input()) map int input() input [0] 0 n = n[::-1] n n[::-1] n ::-1 -1 b0 = [0]*len(n) b0 [0]*len(n) [0] 0 len(n) len n b1 = [0]*len(n) b1 [0]*len(n) [0] 0 len(n) len n b1[0] = 1 b1[0] b1 0 1 for i in range(1, len(n)):
  num = n[i]
  b0[i] = min(b0[i-1]+num, b1[i-1]+num+1)
  b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1)) i range(1, len(n)) range 1 len(n) len n num = n[i] num n[i] n i b0[i] = min(b0[i-1]+num, b1[i-1]+num+1) b0[i] b0 i min(b0[i-1]+num, b1[i-1]+num+1) min b0[i-1]+num b0[i-1] b0 i-1 i 1 num b1[i-1]+num+1 b1[i-1]+num b1[i-1] b1 i-1 i 1 num 1 b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1)) b1[i] b1 i min(b0[i-1]+10-num, b1[i-1]+10-(num+1)) min b0[i-1]+10-num b0[i-1]+10 b0[i-1] b0 i-1 i 1 10 num b1[i-1]+10-(num+1) b1[i-1]+10 b1[i-1] b1 i-1 i 1 10 num+1 num 1 print(min(b0[-1], b1[-1])) print min(b0[-1], b1[-1]) min b0[-1] b0 -1 b1[-1] b1 -1 b1 = [0]*len(n) [0]*len(n) b1 n = [0] + list(map(int, input())) + [0] [0] + list(map(int, input())) + [0] n b0 = [0]*len(n) [0]*len(n) b0 num = n[i] n[i] num n = n[::-1] n[::-1] n
n = [0] + list(map(int, input())) + [0]
n = n[::-1]
#print(n)

b0 = [0]*len(n)
b1 = [0]*len(n)
b1[0] = 1
for i in range(1, len(n)):
  num = n[i]
  b0[i] = min(b0[i-1]+num, b1[i-1]+num+1)
  b1[i] = min(b0[i-1]+10-num, b1[i-1]+10-(num+1))

print(min(b0[-1], b1[-1]))
#print(b0[-1], b1[-1])