def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans
s=input()
ans=y_solver(s)
print(ans) def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans y_solver l=len(tmp) l len(tmp) len tmp int(i) for i in tmp[::-1]+"0" for i in tmp[::-1]+"0" i tmp[::-1]+"0" tmp[::-1] tmp ::-1 -1 "0" for i in tmp[::-1]+"0" int(i) int i rev=[int(i) for i in tmp[::-1]+"0"] rev [int(i) for i in tmp[::-1]+"0"] ans=0 ans 0 for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
     i range(l) range l num=rev[i] num rev[i] rev i next_num=rev[i+1] next_num rev[i+1] rev i+1 i 1 if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
         (num>5) or (num==5 and next_num>=5) num>5 num 5 num==5 and next_num>=5 num==5 num 5 next_num>=5 next_num 5 rev[i+1]+=1 rev[i+1] rev i+1 i 1 1 ans+=min(num,10-num) ans min(num,10-num) min num 10-num 10 num last=rev[l] last rev[l] rev l ans+=min(last,10-last) ans min(last,10-last) min last 10-last 10 last return ans ans tmp tmp s=input() s input() input ans=y_solver(s) ans y_solver(s) y_solver s print(ans) print ans def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans y_solver s=input() input() s ans=y_solver(s) y_solver(s) ans
def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans
s=input()
ans=y_solver(s)
print(ans)