#E
#greedy 
n = input()
l = len(n)

#a is kuriagari naku, pittari na case
#b is kuriagari ari
a,b = 0,1
for v in n:
    v = int(v)
    a_next = min(a+v, b+(10-v))
    b_next = min(a+v+1, b+(10-(v+1)))
    a = a_next
    b = b_next
    
    #繰り上がりが発生する⇔引く数を+1する
    #(10-v)や(10-(v+1)) means charges
    #last keta is pittari, no amari happenes

print(a) n = input() n input() input l = len(n) l len(n) len n a,b = 0,1 a 0 b 1 for v in n:
    v = int(v)
    a_next = min(a+v, b+(10-v))
    b_next = min(a+v+1, b+(10-(v+1)))
    a = a_next
    b = b_next
    
    #繰り上がりが発生する⇔引く数を+1する
    #(10-v)や(10-(v+1)) means charges
    #last keta is pittari, no amari happenes v n v = int(v) v int(v) int v a_next = min(a+v, b+(10-v)) a_next min(a+v, b+(10-v)) min a+v a v b+(10-v) b 10-v 10 v b_next = min(a+v+1, b+(10-(v+1))) b_next min(a+v+1, b+(10-(v+1))) min a+v+1 a+v a v 1 b+(10-(v+1)) b 10-(v+1) 10 v+1 v 1 a = a_next a a_next b = b_next b b_next print(a) print a b = b_next b_next b b_next = min(a+v+1, b+(10-(v+1))) min(a+v+1, b+(10-(v+1))) b_next a = a_next a_next a b = 0,1 1 b n = input() input() n a,b = 0,1 0 a a_next = min(a+v, b+(10-v)) min(a+v, b+(10-v)) a_next v = int(v) int(v) v l = len(n) len(n) l
#E
#greedy 
n = input()
l = len(n)

#a is kuriagari naku, pittari na case
#b is kuriagari ari
a,b = 0,1
for v in n:
    v = int(v)
    a_next = min(a+v, b+(10-v))
    b_next = min(a+v+1, b+(10-(v+1)))
    a = a_next
    b = b_next
    
    #繰り上がりが発生する⇔引く数を+1する
    #(10-v)や(10-(v+1)) means charges
    #last keta is pittari, no amari happenes

print(a)