#include "bits/stdc++.h"

    //region Template
    #include "ext/pb_ds/assoc_container.hpp"

    using namespace __gnu_pbds;
    using namespace std;

    typedef long long ll;
    typedef long double ld;
    typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

    const int MOD = 1000000007;
    using namespace std;
    #define finish(x) cout << x; exit(0);
    #define fastIO cin.tie(0); cin.sync_with_stdio(false);

    void setIn(string s) { freopen(s.c_str(), "r", stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }

    //const string USCAO_TAG = "mountains";
    void usaco(const string& USACO_TAG) { setIn(USACO_TAG + ".in"); setOut(USACO_TAG + ".out"); }
    /*
    template<typename... Args>
    void print(Args... args) {
        #ifdef LOCAL
        ((std::cout << args << " "), ...);
        cout << endl;
        #endif
    }
     */
    //endregion

    /* Notes:
     * Farah </3
     */

    const int MAXN = 1e6 + 5;
    const int INF = 2e9;

    class Matrix {
    public:
        vector<std::vector<int>> _values;
        Matrix(int r, int c)
                : _col(c), _row(r) {
            _values.resize(r);
            for (int i = 0; i < r; ++i){
                _values[i].assign(c, 0);
            }

        }
        void Init(){
            _values.resize(_row);
            for (int i = 0; i < _row; ++i){
                _values[i].resize(_col);
            }
        }
        Matrix() = default;

        Matrix Transpose(){
            Matrix tmp(_col, _row);
            for(int i = 0; i < _row; i++){
                for(int j = 0; j < _col; j++){
                    tmp._values[j][i] = _values[i][j];
                }
            }
            return tmp;
        }
        Matrix operator=(Matrix const& copy);
        Matrix operator*(Matrix const& copy);
        Matrix operator+(Matrix const& copy);
        Matrix operator-(Matrix const& copy);

        friend ostream &operator<<(ostream& str, Matrix& output);
        friend istream &operator>>(istream& str, Matrix& input);

        int _row{};
        int _col{};
    };

    istream &operator>>(istream &str, Matrix &input) {
        str >> input._row;
        str >> input._col;
        //input = Matrix(input._col, input._row);
        input.Init();

        for (int i = 0; i < input._row; ++i)    {
            for (int j = 0; j < input._col; ++j)
                str >> input._values[i][j];
        }
        return str;
    }
    ostream &operator<<(ostream &out, Matrix &m)
    {
        //out << m._row << ' ' << m._col << '\n';
        for (int i = 0; i < m._row; ++i){
            for (int j = 0; j < m._col; ++j)
                out << m._values[i][j] << (j != m._col - 1 ? " " : "");
            out << (i != m._row - 1 ? "\n" : "");
        }
        return out;
    }

    Matrix Matrix::operator*(Matrix const &copy) {
        if(_col != copy._row){
            Matrix tmp(_row, _col);
            for(int i = 0; i < _row; i++){
                for(int j = 0; j < _col; j++){
                    tmp._values[i][j] = _values[i][j];
                }
            }
            return tmp;
        }
        Matrix tmp(_row, copy._col);
    /*    cout << "TEST\n";
        for (int i = 0; i < copy._row; ++i)    {
            for (int j = 0; j < copy._col; ++j)
                cout << copy._values[i][j] << " ";
            cout << endl;
        }*/
        for(int i = 0; i < _row; i++){
            for(int j = 0; j < copy._col; j++){
                for(int k = 0; k < _col; k++) {
                    tmp._values[i][j] += _values[i][k] * copy._values[k][j];
                    //printf("[%d, %d] = %d * %d\n", i, j, _values[i][k], copy._values[k][j] );
                }
            }
        }
        return tmp;
    }
    Matrix Matrix::operator=(Matrix const &copy) {
        for(int i = 0; i < _row; i++){
            for(int j = 0; j < _col; j++){
                _values[i][j] = copy._values[i][j];
            }
        }
        return *this;
    }
    Matrix Matrix::operator+(Matrix const &copy) {
        Matrix tmp(_row, _col);
        for(int i = 0; i < _row; i++){
            for(int j = 0; j < _col; j++){
                tmp._values[i][j] = _values[i][j] + copy._values[i][j];
            }
        }
        return tmp;
    }
    Matrix Matrix::operator-(Matrix const &copy) {
        Matrix tmp(_row, _col);
        for(int i = 0; i < _row; i++){
            for(int j = 0; j < _col; j++){
                tmp._values[i][j] = _values[i][j] - copy._values[i][j];
            }
        }
        return tmp;
    }

    class SquareMatrix : public Matrix {
    public:
        SquareMatrix(int n){
            Matrix(n, n);
        }
        SquareMatrix() {}

        friend istream &operator>>(istream& str, SquareMatrix& input);
    };
    istream &operator>>(istream &str, SquareMatrix &input) {
        str >> input._row;
        input._col = input._row;
        input.Init();

        for (int i = 0; i < input._row; ++i)    {
            for (int j = 0; j < input._col; ++j)
                str >> input._values[i][j];
        }
        return str;
    }

    ll dp[MAXN][2];

    int main() {
        //usaco("milkvisits");

    #ifdef LOCAL
        setIn("in.txt");
        setOut("out.txt");
    #endif

        string s;
        cin >> s;

        //memset(dp, INF, sizeof dp);
        dp[s.size()][1] = INF;
        for(int i = s.size() - 1; i >= 0; i--){
            int x = s[i] - '0';

            dp[i][0] = dp[i][1] = INF;
            for(int j = 0; j < 2; j++){
                int nx = x + j;

                if(nx < 10){
                    dp[i][0] = min(dp[i][0], dp[i + 1][j] + nx);
                }
                dp[i][1] = min(dp[i][1], dp[i + 1][j] + 10 - nx);
            }
        }
        cout << min(dp[0][0], dp[0][1] + 1) << "\n";

    }

Measured memory_bytes: 17028