#

import sys
input=sys.stdin.readline

def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print(min(dp[-1]))
    
if __name__=="__main__":
    main() import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print(min(dp[-1]))
     main NS=(input().strip("\n"))[::-1] NS (input().strip("\n"))[::-1] input().strip("\n") ().strip () input strip "\n" ::-1 -1 [0]*2 for i in range(len(NS)) for i in range(len(NS)) i range(len(NS)) range len(NS) len NS for i in range(len(NS)) [0]*2 [0] 0 2 dp=[[0]*2 for i in range(len(NS))] dp [[0]*2 for i in range(len(NS))] dp[0][0]=int(NS[0]) dp[0][0] [0] dp 0 0 int(NS[0]) int NS[0] NS 0 dp[0][1]=10-int(NS[0]) dp[0][1] [0] dp 0 1 10-int(NS[0]) 10 int(NS[0]) int NS[0] NS 0 for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
     i range(1,len(NS)) range 1 len(NS) len NS d=int(NS[i]) d int(NS[i]) int NS[i] NS i dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1) dp[i][0] [i] dp i 0 min(dp[i-1][0]+d,dp[i-1][1]+d+1) min dp[i-1][0]+d dp[i-1][0] [i-1] dp i-1 i 1 0 d dp[i-1][1]+d+1 dp[i-1][1]+d dp[i-1][1] [i-1] dp i-1 i 1 1 d 1 dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d) dp[i][1] [i] dp i 1 min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d) min dp[i-1][0]+10-d dp[i-1][0]+10 dp[i-1][0] [i-1] dp i-1 i 1 0 10 d dp[i-1][1]-1+10-d dp[i-1][1]-1+10 dp[i-1][1]-1 dp[i-1][1] [i-1] dp i-1 i 1 1 1 10 d dp[-1][1]+=1 dp[-1][1] [-1] dp -1 1 1 print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 if __name__=="__main__":
    main() __name__=="__main__" __name__ "__main__" main() main input=sys.stdin.readline sys.stdin.readline input def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print(min(dp[-1]))
     def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print(min(dp[-1]))
     main
#

import sys
input=sys.stdin.readline

def main():
    NS=(input().strip("\n"))[::-1]
    dp=[[0]*2 for i in range(len(NS))]
    # 0:おつり0 1:おつり10-d
    dp[0][0]=int(NS[0])
    dp[0][1]=10-int(NS[0])
    for i in range(1,len(NS)):
        d=int(NS[i])
        dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+d+1)
        dp[i][1]=min(dp[i-1][0]+10-d,dp[i-1][1]-1+10-d)
    dp[-1][1]+=1
    print(min(dp[-1]))
    
if __name__=="__main__":
    main()
