N=input()
a=[]
s=len(N)

for i in range(s-1,-1,-1):
    a.append(int(N[i]))
a.append(0)
#print(a)

p=sum(a)
t=0
f=0
c=0
for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
        
print(t+f+c) N=input() N input() input a=[] a [] s=len(N) s len(N) len N for i in range(s-1,-1,-1):
    a.append(int(N[i])) i range(s-1,-1,-1) range s-1 s 1 -1 -1 a.append(int(N[i])) a.append a append int(N[i]) int N[i] N i a.append(0) a.append a append 0 p=sum(a) p sum(a) sum a t=0 t 0 f=0 f 0 c=0 c 0 for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
         i range(len(a)-1) range len(a)-1 len(a) len a 1 if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
         a[i] <= 5 a[i] a i 5 t+=a[i] t a[i] a i if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
         a[i] == 5 and a[i+1]>=5 a[i] == 5 a[i] a i 5 a[i+1]>=5 a[i+1] a i+1 i 1 5 f=0 f 0 c+=10-a[i] c 10-a[i] 10 a[i] a i a[i+1] += 1 a[i+1] a i+1 i 1 1 f=1 f 1 print(t+f+c) print t+f+c t+f t f c s=len(N) len(N) s N=input() input() N t+=a[i] a[i] t c+=10-a[i] 10-a[i] c a=[] [] a t=0 0 t f=0 0 f p=sum(a) sum(a) p c=0 0 c f=1 1 f f=0 0 f
N=input()
a=[]
s=len(N)

for i in range(s-1,-1,-1):
    a.append(int(N[i]))
a.append(0)
#print(a)

p=sum(a)
t=0
f=0
c=0
for i in range(len(a)-1):
    if a[i] <= 5:
        t+=a[i]
        if a[i] == 5 and a[i+1]>=5:a[i+1]+=1
        f=0
        
    else:
        c+=10-a[i]
        a[i+1] += 1
        f=1
        
print(t+f+c)