#include <bits/stdc++.h>
using namespace std;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1<<30;
const ll mod = 1000000007LL;

int main() {
  string S;
  cin>>S;
  reverse(S.begin(),S.end());
  int l = S.size();
  int ans = 0;
  int over = 0;
  for(int i = 0; i < l; i++){
    int t = S[i]-'0';
    t+=over;
    over = 0;
    if(t>=10) {
      t-=10;
      over = 1;
    }
    if(t<=4) ans += t;
    if(t==5){
      int s;
      if(i!=l-1) s = S[i+1]-'0';
      if(i==l-1||s<=4) ans+=t;
      else if(s>=5) {
        ans+=(10-t);
        over=1;
      }
    }
    if(t>=6) {
      ans += (10-t);
      over = 1;
    }
    if(i==l-1&&over!=0) ans+=over;
  }
  cout<<ans<<endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i,r) for (ll i = 0; i < (ll)(r); i++)

ll dp[1000010][2];

int main(){
    string s; cin >> s;
    s = '0' + s;
    int n = s.size();
    dp[0][1] = 1;
    for(int i=1;i<n;i++){
        int c = s[i]-'0';
        dp[i][0] = min(dp[i-1][0]+c, dp[i-1][1]+(10-c) );
        dp[i][1] = min(dp[i-1][0]+c+1, dp[i-1][1]+(10-c-1));
    }
    cout << dp[n-1][0] << endl;
}