list_n=[0]+list(map(int,input()))
l = len(list_n)

pay=0
back=0
for i in range(l-1,-1,-1):
    #l-1から0まで
    now = list_n[i]
    if now in [0,1,2,3,4] or (now == 5 and list_n[i-1]<5):
        pay+=now
        list_n[i]=0
    elif now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5):
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1
print(pay+back) list_n=[0]+list(map(int,input())) list_n [0]+list(map(int,input())) [0] 0 list(map(int,input())) list map(int,input()) map int input() input l = len(list_n) l len(list_n) len list_n pay=0 pay 0 back=0 back 0 for i in range(l-1,-1,-1):
    #l-1から0まで
    now = list_n[i]
    if now in [0,1,2,3,4] or (now == 5 and list_n[i-1]<5):
        pay+=now
        list_n[i]=0
    elif now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5):
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1 i range(l-1,-1,-1) range l-1 l 1 -1 -1 now = list_n[i] now list_n[i] list_n i if now in [0,1,2,3,4] or (now == 5 and list_n[i-1]<5):
        pay+=now
        list_n[i]=0
    elif now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5):
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1 now in [0,1,2,3,4] or (now == 5 and list_n[i-1]<5) now in [0,1,2,3,4] now [0,1,2,3,4] 0 1 2 3 4 now == 5 and list_n[i-1]<5 now == 5 now 5 list_n[i-1]<5 list_n[i-1] list_n i-1 i 1 5 pay+=now pay now list_n[i]=0 list_n[i] list_n i 0 elif now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5):
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
     now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5) now in [6,7,8,9] now [6,7,8,9] 6 7 8 9 now == 5 and list_n[i-1]>=5 now == 5 now 5 list_n[i-1]>=5 list_n[i-1] list_n i-1 i 1 5 back+=10-now back 10-now 10 now list_n[i]=0 list_n[i] list_n i 0 list_n[i-1] +=1 list_n[i-1] list_n i-1 i 1 1 elif now==10:
        list_n[i]=0
        list_n[i-1] +=1 now==10 now 10 list_n[i]=0 list_n[i] list_n i 0 list_n[i-1] +=1 list_n[i-1] list_n i-1 i 1 1 print(pay+back) print pay+back pay back pay=0 0 pay l = len(list_n) len(list_n) l list_n=[0]+list(map(int,input())) [0]+list(map(int,input())) list_n back=0 0 back back+=10-now 10-now back now = list_n[i] list_n[i] now pay+=now now pay
list_n=[0]+list(map(int,input()))
l = len(list_n)

pay=0
back=0
for i in range(l-1,-1,-1):
    #l-1から0まで
    now = list_n[i]
    if now in [0,1,2,3,4] or (now == 5 and list_n[i-1]<5):
        pay+=now
        list_n[i]=0
    elif now in [6,7,8,9] or (now == 5 and list_n[i-1]>=5):
        back+=10-now
        list_n[i]=0
        list_n[i-1] +=1
    elif now==10:
        list_n[i]=0
        list_n[i-1] +=1
print(pay+back)