Problem:
Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
#include <unordered_set>
#define rep(i,n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define Sort(a) sort(a.begin(),a.end())
#define cSort(a,cmp) sort(a.begin(),a.end(),cmp)
#define reSort(a) sort(a.rbegin(), a.rend())
static const ll llMAX = numeric_limits<long long>::max();
static const int intMAX = numeric_limits<int>::max();
static const ll  llMIN = numeric_limits<long long>::min();
static const int intMIN = numeric_limits<int>::min();
static const ll d_5 = 100000;
static const ll d9_7 = 1000000007;
static const ll d_9 = 1000000000;
static const double PI=3.14159265358979323846;
template<class T>
void Printvector(std::vector<T> a){
  int size = a.size();
  rep(i,size){
    cout<<a[i]<<" ";
  }
  cout<<endl;
}
template<class T>
void Printvector(std::vector<std::vector<T>> a){
  int size = a.size();
  rep(i,size){
    int size2=a[i].size();
    rep(j,size2){
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }
  cout<<endl;
}
ll digitpower(ll a,ll b){//aのb乗を計算
  if(b==1){
    return a;
  }else if(b==0){
    return 1;
  }
  int mode=0;
  if(b%2==1){
    ll tmp = digitpower(a,(b-1)/2);
    if(mode==1){
      tmp%=d9_7;
    }
    tmp*=tmp;
    if(mode==1){
      tmp%=d9_7;
    }
    tmp*=a;
    if(mode==1){
      return tmp%d9_7;
    }else{
      return tmp;
    }
  }else{
    ll tmp = digitpower(a,(b)/2);
    if(mode==1){
      tmp%=d9_7;
    }
    tmp*=tmp;
    if(mode==1){
      tmp%=d9_7;
    }
    if(mode==1){
      return tmp%d9_7;
    }else{
      return tmp;
    }
  }
}
vl facs(2000010,-1);
ll Factrial(ll num){
  if(facs[num]!=-1){
    return facs[num];
  }
  if(num==1||num<=0){
    return 1;
  }else if(num<0){
    printf("ERROR_minus\n");
    return 0;
  }else{
    facs[num]=(num*Factrial(num-1))%d9_7;
    return facs[num];
  }
}
long long modinv(long long a, long long m) {//modの逆元

    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}

vl invs(2000010,-1);
ll linercomb(ll n,ll k, ll mod){//n,kの線形時間で求める
  if(n<k)return 0;
  if(n<0)return 0;
  if(k==0 || k==n)return 1;
  ll ans=Factrial(n);
  if(invs[k]==-1){
    invs[k]=modinv(Factrial(k),mod);
  }
  ans*=invs[k];
  ans%=d9_7;
  ll k1=Factrial(n-k);
  k1%=mod;
  ans*=modinv(k1,mod);
  ans%=mod;
  return ans;
}
unordered_map<ll,ll> prime_factor(int64_t n) {
  unordered_map<ll,ll> ret;
  for(int64_t i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

template<class T>
vector<T> getaccum(vector<T> a){
  int size=a.size();
  vector<T> ans(size);
  ans[0]=a[0];
  for(int i=0;i<size-1;i++){
    ans[i+1]=ans[i]+a[i+1];
    //ans[i+1]%=d9_7;
  }
  return ans;
}

ll getaccumnum(vector<ll> accum,int l,int r){//閉区間[l,r]の総和
  if(l==0){
    return accum[r];
  }else{
    return accum[r]-accum[l-1];
  }
}

struct datas{
  int a;
  int b;
};/*
bool cmp(const datas &a, const datas &b)
{
    return a.num < b.num;
}*/
//Segment Tree
ll Digit_DP(string s){
  ll size=s.size();
  int mas=2;
  vector<vl> dp(size+1,vl(mas,llMAX/100));
  // dp[ 決めた桁数 ][ より大きい ] := 総数
  dp[0][0]=0;
  dp[0][1]=0;
  for(int i=0;i<size;i++){
    int D=s[i]-'0';
    for(int j=0;j<2;j++){
      for(int d=9;d>=(j ? D:0);d--){
        //dを選択したときの遷移
        if(D>=d){
          dp[i+1][j||(d>D)]=min(dp[i+1][j||(d>D)],dp[i][j]+D-d  +  d);//払う自身の枚数も足す
        }else{
          dp[i+1][j||(d>D)]=min(dp[i+1][j||(d>D)],(10-(d-D))+dp[i][j]-1  +  d);
        }
      }
      
    }
  }
  Printvector(dp);
  return min(dp[size][0],dp[size][1]);
}
int main(void){
  string s;
  cin>>s;
  vl nums={0,1,2,3,4,5,5,4,3,2};
  ll ans=0;
  vl ns;
  int flag=0;
  rep(i,s.size()){
    if(i==0){
      
    ans+=nums[s[i]-'0'];
    
    }else{
      if(s[i]-'0'<=5){
        ans+=s[i]-'0';
      }else{
        if(s[i-1]-'0'>=6){
          ans+=9-(s[i]-'0');
        }else{
          //cout<<"e";
          ans+=nums[s[i]-'0'];
        }
      }
    }
    ns.pb(nums[s[i]-'0']);

  }
  cout<<ans<<endl;
  return 0;
}

//<<std::setprecision(30)

//重複削除
 /* std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());*/