#include<bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N=1e6+5;
const int MOD=1e9+7;

typedef long long ll;
typedef long double ld;

using namespace std;

string s;
int n, dp[N][10][2][2];

int solve(int i, int dig, int islesser, int carryright)
{
    if(i<0 && islesser)
        return 1;
    else if(i<0)
        return 0;
    if(dp[i][dig][islesser][carryright]!=-1)
        return dp[i][dig][islesser][carryright];
    if(islesser)
    {
        int ret=1e8;
        if(!carryright)
        {
            for(int a=(s[i]-'0')+1; a<=9; a++)
                ret=min(ret, solve(i-1, a, 0, 0) + a + a - (s[i]-'0'));
            ret=min(ret, solve(i-1, (s[i]-'0'), 1, 0) + (s[i]-'0'));
            for(int a=0; a<(s[i]-'0'); a++)
                ret=min(ret, solve(i-1, a, 1, 1) + a + 10 + a - (s[i]-'0'));
        }
        else
        {
            for(int a=(s[i]-'0')+1; a<=9; a++)
                ret=min(ret, solve(i-1, a, 0, 0) + a + a - 1 - (s[i]-'0'));
            ret=min(ret, solve(i-1, (s[i]-'0'), 1, 1) + (s[i]-'0') + 9);
            for(int a=0; a<(s[i]-'0'); a++)
                ret=min(ret, solve(i-1, a, 1, 1) + a + 9 + a - (s[i]-'0'));
        }
        // if(ret<0)
        //     printf("%d %d %d %d %d\n", i, dig, islesser, carryright, ret);
        return dp[i][dig][islesser][carryright]=ret;
    }
    else
    {
        int ret=1e8;
        if(!carryright)
        {
            for(int a=(s[i]-'0'); a<=9; a++)
                ret=min(ret, solve(i-1, a, 0, 0) + a + a - (s[i]-'0'));
            for(int a=0; a<(s[i]-'0'); a++)
                ret=min(ret, solve(i-1, a, 1, 1) + a + 10 + a - (s[i]-'0'));
        }
        else
        {
            exit(7);
        }
        // if(ret<0)
        //     printf("%d %d %d %d %d\n", i, dig, islesser, carryright, ret);
        return dp[i][dig][islesser][carryright]=ret;
    }
}

int main()
{
    cin>>s;
    n=s.length();
    memset(dp, -1, sizeof(dp));
    int ans=1e8;
    for(int a=0; a<(s[n-1]-'0'); a++)
    {
        // printf("%d %d\n", a, (s[n-1]-'0'));
        ans=min(ans, solve(n-2, a, 1, 1) + a + 10 + a - (s[n-1]-'0'));
        // if(ans<0)
        //     printf("%d %d %d %d %d\n", n-1, a, 1, 1, ans);
    }
    for(int a=(s[n-1]-'0'); a<=9; a++)
    {
        ans=min(ans, solve(n-2, a, 0, 0) + a + a - (s[n-1]-'0'));
        // if(ans<0)
        //     printf("%d %d %d %d %d\n", n-1, a, 1, 1, ans);
    }
    printf("%d\n", ans);
    // while(s[n-1]=='0')
    //     n--;
    // printf("%d\n", n);
    // pref[0]=(s[0]-'0');
    // for(int a=1; a<n; a++)
    //     pref[a]=pref[a-1]+(s[a]-'0');
    // printf("%d\n", pref[n-1]);
    // int curdiff=0;
    // int ans=pref[n-1];
    // ans=min(ans, pref[n-2]+1+10-(s[n-1]-'0'));
    // for(int a=n-3; a>=0; a--)
    // {
    //     curdiff+=9-(s[a+1]-'0');
    //     ans=min(ans, pref[a]+1+curdiff);
    // }
    // printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 235780