#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=input()
l=len(n)
ans=0
d=[(0,0)]*l
d[0]=min(int(n[0]),10-int(n[0])+1),min(int(n[0])+1,10-int(n[0]))
for i in range(1,l):
    ori=min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1])
    ori_1=min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1])
    d[i]=ori,ori_1
print(d[l-1][0]) import math math import itertools itertools from collections import deque from collections import Counter import bisect bisect n=input() n input() input l=len(n) l len(n) len n ans=0 ans 0 d=[(0,0)]*l d [(0,0)]*l [(0,0)] (0,0) 0 0 l d[0]=min(int(n[0]),10-int(n[0])+1),min(int(n[0])+1,10-int(n[0])) d[0] d 0 d[0]=min(int(n[0]),10-int(n[0])+1),min(int(n[0])+1,10-int(n[0])) min(int(n[0]),10-int(n[0])+1) min int(n[0]) int n[0] n 0 10-int(n[0])+1 10-int(n[0]) 10 int(n[0]) int n[0] n 0 1 min(int(n[0])+1,10-int(n[0])) min int(n[0])+1 int(n[0]) int n[0] n 0 1 10-int(n[0]) 10 int(n[0]) int n[0] n 0 for i in range(1,l):
    ori=min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1])
    ori_1=min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1])
    d[i]=ori,ori_1 i range(1,l) range 1 l ori=min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1]) ori min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1]) min int(n[i])+d[i-1][0] int(n[i]) int n[i] n i d[i-1][0] [i-1] d i-1 i 1 0 10-int(n[i])+d[i-1][1] 10-int(n[i]) 10 int(n[i]) int n[i] n i d[i-1][1] [i-1] d i-1 i 1 1 ori_1=min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1]) ori_1 min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1]) min int(n[i])+1+d[i-1][0] int(n[i])+1 int(n[i]) int n[i] n i 1 d[i-1][0] [i-1] d i-1 i 1 0 10-int(n[i])-1+d[i-1][1] 10-int(n[i])-1 10-int(n[i]) 10 int(n[i]) int n[i] n i 1 d[i-1][1] [i-1] d i-1 i 1 1 d[i]=ori,ori_1 d[i] d i d[i]=ori,ori_1 ori ori_1 print(d[l-1][0]) print d[l-1][0] [l-1] d l-1 l 1 0 d=[(0,0)]*l [(0,0)]*l d ori_1=min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1]) min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1]) ori_1 ans=0 0 ans l=len(n) len(n) l ori=min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1]) min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1]) ori n=input() input() n
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=input()
l=len(n)
ans=0
d=[(0,0)]*l
d[0]=min(int(n[0]),10-int(n[0])+1),min(int(n[0])+1,10-int(n[0]))
for i in range(1,l):
    ori=min(int(n[i])+d[i-1][0],10-int(n[i])+d[i-1][1])
    ori_1=min(int(n[i])+1+d[i-1][0],10-int(n[i])-1+d[i-1][1])
    d[i]=ori,ori_1
print(d[l-1][0])