import sys
def main():
    input=sys.stdin.readline
    S=input().strip()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0])

if __name__=="__main__":
    main() import sys sys def main():
    input=sys.stdin.readline
    S=input().strip()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0]) main input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline S=input().strip() S input().strip() ().strip () input strip [0,0] for i in range(len(S)+1) for i in range(len(S)+1) i range(len(S)+1) range len(S)+1 len(S) len S 1 for i in range(len(S)+1) [0,0] 0 0 dp=[[0,0] for i in range(len(S)+1)] dp [[0,0] for i in range(len(S)+1)] dp[0][1]=1 dp[0][1] [0] dp 0 1 1 for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

     i range(1,len(S)+1) range 1 len(S)+1 len(S) len S 1 for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

     j (0,1) 0 1 if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

     j==0 j 0 dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1])) dp[i][0] [i] dp i 0 min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1])) min dp[i-1][0]+int(S[i-1]) dp[i-1][0] [i-1] dp i-1 i 1 0 int(S[i-1]) int S[i-1] S i-1 i 1 dp[i-1][1]+10-int(S[i-1]) dp[i-1][1]+10 dp[i-1][1] [i-1] dp i-1 i 1 1 10 int(S[i-1]) int S[i-1] S i-1 i 1 elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

     j==1 j 1 dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1) dp[i][1] [i] dp i 1 min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1) min dp[i-1][0]+int(S[i-1])+1 dp[i-1][0]+int(S[i-1]) dp[i-1][0] [i-1] dp i-1 i 1 0 int(S[i-1]) int S[i-1] S i-1 i 1 1 dp[i-1][1]+10-int(S[i-1])-1 dp[i-1][1]+10-int(S[i-1]) dp[i-1][1]+10 dp[i-1][1] [i-1] dp i-1 i 1 1 10 int(S[i-1]) int S[i-1] S i-1 i 1 1 print(dp[len(S)][0]) print dp[len(S)][0] [len(S)] dp len(S) len S 0 if __name__=="__main__":
    main() __name__=="__main__" __name__ "__main__" main() main def main():
    input=sys.stdin.readline
    S=input().strip()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0]) def main():
    input=sys.stdin.readline
    S=input().strip()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0]) main
import sys
def main():
    input=sys.stdin.readline
    S=input().strip()
    dp=[[0,0] for i in range(len(S)+1)]
    dp[0][1]=1
    for i in range(1,len(S)+1):
        for j in (0,1):
            if j==0:
                dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
            elif j==1:
                dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)

    print(dp[len(S)][0])

if __name__=="__main__":
    main()