def main():
    N = input()
    len_N = len(N)
    ans = 0
    pre = 0
    for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
    print(ans)



if __name__ == "__main__":
    main() def main():
    N = input()
    len_N = len(N)
    ans = 0
    pre = 0
    for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
    print(ans) main N = input() N input() input len_N = len(N) len_N len(N) len N ans = 0 ans 0 pre = 0 pre 0 for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
     i range(len_N-1,-1,-1) range len_N-1 len_N 1 -1 -1 int_N = int(N[i]) + pre int_N int(N[i]) + pre int(N[i]) int N[i] N i pre if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
     int_N <= 4 int_N 4 ans += int_N ans int_N pre = 0 pre 0 elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
         int_N >= 6 int_N 6 ans += 10 - int_N ans 10 - int_N 10 int_N pre = 1 pre 1 elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
     int_N == 5 int_N 5 if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
     i == 0 i 0 ans += int_N ans int_N pre = 0 pre 0 if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
     int(N[i-1]) <= 4 int(N[i-1]) int N[i-1] N i-1 i 1 4 ans += int_N ans int_N pre = 0 pre 0 ans += int_N ans int_N pre = 1 pre 1 ans += pre ans pre print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N = input()
    len_N = len(N)
    ans = 0
    pre = 0
    for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
    print(ans) def main():
    N = input()
    len_N = len(N)
    ans = 0
    pre = 0
    for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
    print(ans) main
def main():
    N = input()
    len_N = len(N)
    ans = 0
    pre = 0
    for i in range(len_N-1,-1,-1):
        int_N = int(N[i]) + pre
        if int_N <= 4:
            ans += int_N
            pre = 0
        elif int_N >= 6:
            ans += 10 - int_N
            pre = 1
        elif int_N == 5:
            if i == 0:
                ans += int_N
                pre = 0
            else:
                if int(N[i-1]) <= 4:
                    ans += int_N
                    pre = 0
                else:
                    ans += int_N
                    pre = 1
    else:
        ans += pre
    print(ans)



if __name__ == "__main__":
    main()