#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

L=list(map(int,input())); n=len(L)
L.reverse();L.append(0)
for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

print(count + sum(L))
# print(L)
    
 import sys, math, itertools, collections, bisect sys math itertools collections bisect input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') input lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') sys.stdin.buffer.readline().rstrip().decode('utf-8') sys.stdin.buffer.readline().rstrip().decode sys.stdin.buffer.readline().rstrip() sys.stdin.buffer.readline().rstrip sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline rstrip decode 'utf-8' inf = float('inf') inf float('inf') float 'inf' mod = 10**9+7 mod 10**9+7 10**9 10 9 7 mans = inf mans inf ans = 0 ans 0 count = 0 count 0 pro = 1 pro 1 L=list(map(int,input())) L list(map(int,input())) list map(int,input()) map int input() input n=len(L) n len(L) len L L.reverse() L.reverse L reverse L.append(0) L.append L append 0 for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1 i range(n) range n if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1 L[i] >= 6 L[i] L i 6 count += 10-L[i] count 10-L[i] 10 L[i] L i L[i] = 0 L[i] L i 0 L[i+1] += 1 L[i+1] L i+1 i 1 1 elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1 L[i] == 5 and L[i+1] >= 5 L[i] == 5 L[i] L i 5 L[i+1] >= 5 L[i+1] L i+1 i 1 5 count += 10-L[i] count 10-L[i] 10 L[i] L i L[i] = 0 L[i] L i 0 L[i+1] += 1 L[i+1] L i+1 i 1 1 print(count + sum(L)) print count + sum(L) count sum(L) sum L count = 0 0 count mans = inf inf mans count += 10-L[i] 10-L[i] count inf = float('inf') float('inf') inf L=list(map(int,input())) list(map(int,input())) L input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') input mod = 10**9+7 10**9+7 mod ans = 0 0 ans count += 10-L[i] 10-L[i] count pro = 1 1 pro n=len(L) len(L) n
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

L=list(map(int,input())); n=len(L)
L.reverse();L.append(0)
for i in range(n):

  if L[i] >= 6:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

  elif L[i] == 5 and L[i+1] >= 5:
    count += 10-L[i]
    L[i] = 0; L[i+1] += 1

print(count + sum(L))
# print(L)
    

