#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;

#define int long long
#define cint cpp_int
#define endl "\n"
#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPR(i,a,n) for(int i=a;i>n;--i)
#define RUP(a,b) ((a+b-1)/(b))
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX/2
#define PI acos(-1.0)

typedef long long ll;
typedef pair<int,int> Pii;
typedef tuple<int,int,int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<Pii> VPii;
typedef vector<string> Vs;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> void YesNo(T a) {cout<<(a?"Yes":"No")<<endl;}
template<class T> void YESNO(T a) {cout<<(a?"YES":"NO")<<endl;}
void vout(Vi &v){for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";cout<<endl;}
void vvout(VVi &v){for(int i=0;i<(v).size();i++) vout(v[i]);}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int mypow(int x, int n, int m){if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}
int digit(int k,int i){string s = to_string(k);return s[s.size()-i]-'0';}
void uniq(Vi &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());}
int ctoi(char c){if(c>='0'&&c<='9'){return c-'0';}return 0;}
Vi accum(Vi v){Vi ret((v).size());ret[0]=v[0];REP(i,1,(v).size()) ret[i]=ret[i-1]+v[i];return ret;}
bool comp(Pii a,Pii b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
signed main(){
    string s;
    cin>>s;
    int n=s.size();
    VVi dp(n+1,Vi(2,INF));
    dp[0][0]=0;
    dp[0][1]=2;

    REP(i,0,n){
        // if(s[i]=='0'){
        //     dp[i+1][0]=dp[i][0];
        //     dp[i+1][1]=dp[i][1];
        //     continue;
        // }
        chmin(dp[i+1][0],dp[i][0]+ctoi(s[i]));
        chmin(dp[i+1][0],dp[i][1]+ctoi(s[i]));
        chmin(dp[i+1][1],dp[i][1]+(10-ctoi(s[i]))-1);
        chmin(dp[i+1][1],dp[i][0]+ctoi(s[i])+2);
        chmin(dp[i+1][1],dp[i][1]+ctoi(s[i])+2);
    }
//    vvout(dp);
    cout<<min(dp[n][0],dp[n][1])<<endl;

//     while(s[s.size()-1]=='0'){
//         s.pop_back();
//     }
//     reverse(ALL(s));
//     n=s.size();
//     VVi dp(n,Vi(2,INF));
//     dp[0][0]=ctoi(s[0]);
//     dp[0][1]=11-ctoi(s[0]);
//     REP(i,1,n){
//         // if(s[i]=='0'){
//         //     dp[i][0]=dp[i-1][0];
//         //     dp[i][1]=dp[i-1][1];
//         //     continue;
//         // }
//         chmin(dp[i][0],dp[i-1][0]+ctoi(s[i]));
//         if(s[i]!='9') chmin(dp[i][0],dp[i-1][1]+ctoi(s[i]));
//         chmin(dp[i][1],dp[i-1][0]+11-ctoi(s[i]));
//         chmin(dp[i][1],dp[i-1][1]+9-ctoi(s[i]));
//     }
// //   vvout(dp);
// //    cout<<dp[n-1][0]<<" "<<dp[n-1][1]<<endl;
//      cout<<min(dp[n-1][0],dp[n-1][1])<<endl;
    return 0;
}


Measured memory_bytes: 55940