def main():
    N=[int(x) for x in input()]
    l=len(N)+1
    DP=[[0]*2 for _ in range(l)]
    DP[0][1]=1
    for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
    print(DP[-1][0])
    
if __name__=='__main__':
    main() def main():
    N=[int(x) for x in input()]
    l=len(N)+1
    DP=[[0]*2 for _ in range(l)]
    DP[0][1]=1
    for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
    print(DP[-1][0])
     main int(x) for x in input() for x in input() x input() input for x in input() int(x) int x N=[int(x) for x in input()] N [int(x) for x in input()] l=len(N)+1 l len(N)+1 len(N) len N 1 [0]*2 for _ in range(l) for _ in range(l) _ range(l) range l for _ in range(l) [0]*2 [0] 0 2 DP=[[0]*2 for _ in range(l)] DP [[0]*2 for _ in range(l)] DP[0][1]=1 DP[0][1] [0] DP 0 1 1 for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
     i n enumerate(N) enumerate N DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n)) DP[i+1][0] [i+1] DP i+1 i 1 0 min(DP[i][0]+n,DP[i][1]+(10-n)) min DP[i][0]+n DP[i][0] [i] DP i 0 n DP[i][1]+(10-n) DP[i][1] [i] DP i 1 10-n 10 n DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1))) DP[i+1][1] [i+1] DP i+1 i 1 1 min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1))) min DP[i][0]+(n+1) DP[i][0] [i] DP i 0 n+1 n 1 DP[i][1]+(10-(n+1)) DP[i][1] [i] DP i 1 10-(n+1) 10 n+1 n 1 print(DP[-1][0]) print DP[-1][0] [-1] DP -1 0 if __name__=='__main__':
    main() __name__=='__main__' __name__ '__main__' main() main def main():
    N=[int(x) for x in input()]
    l=len(N)+1
    DP=[[0]*2 for _ in range(l)]
    DP[0][1]=1
    for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
    print(DP[-1][0])
     def main():
    N=[int(x) for x in input()]
    l=len(N)+1
    DP=[[0]*2 for _ in range(l)]
    DP[0][1]=1
    for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
    print(DP[-1][0])
     main
def main():
    N=[int(x) for x in input()]
    l=len(N)+1
    DP=[[0]*2 for _ in range(l)]
    DP[0][1]=1
    for i,n in enumerate(N):
        DP[i+1][0]=min(DP[i][0]+n,DP[i][1]+(10-n))
        DP[i+1][1]=min(DP[i][0]+(n+1),DP[i][1]+(10-(n+1)))
    print(DP[-1][0])
    
if __name__=='__main__':
    main()