def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
      
if __name__ == '__main__':
    main() def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
       main n=list(map(int,list(input())[::-1]))+[0] n list(map(int,list(input())[::-1]))+[0] list(map(int,list(input())[::-1])) list map(int,list(input())[::-1]) map int list(input())[::-1] (input()) list input() input ::-1 -1 [0] 0 nl=len(n) nl len(n) len n ans=0 ans 0 for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
     i range(nl-1) range nl-1 nl 1 if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
     n[i]>=6 or (n[i]==5 and n[i+1]>=5) n[i]>=6 n[i] n i 6 n[i]==5 and n[i+1]>=5 n[i]==5 n[i] n i 5 n[i+1]>=5 n[i+1] n i+1 i 1 5 n[i]=10-n[i] n[i] n i 10-n[i] 10 n[i] n i n[i+1]+=1 n[i+1] n i+1 i 1 1 print(sum(n)) print sum(n) sum n if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
       def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
       main
def main():
    n=list(map(int,list(input())[::-1]))+[0]
    nl=len(n)
    ans=0
    for i in range(nl-1):
        if n[i]>=6 or (n[i]==5 and n[i+1]>=5):
            n[i]=10-n[i]
            n[i+1]+=1
    print(sum(n))
      
if __name__ == '__main__':
    main()