#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using std::string;
using std::vector;
using std::set;
using std::multiset;
using std::unordered_set;
using std::map;
using std::multimap;
using std::unordered_map;
using std::pair;
using std::cin;
using std::cout;

using boost::multiprecision::cpp_int;
using cpp_dec_float_1000 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000>>;

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

constexpr char CRLF  = '\n';
constexpr char SPACE = ' ';
constexpr char VECTOR_COUT_SEPARATOR = SPACE;
constexpr ll INF = 1000'000'007;
constexpr int MOD = 1000'000'007;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; }
template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; }
template<class T> T modinv(T a, T m) { T b = m, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; boost::swap(a, b); u -= t * v; boost::swap(u, v); } u %= m; if (u < 0) u += m; return u; }

struct Tmp
{
public:
  int N;
  vector<char> S;
  vector<int> dp;
  vector<int> kuriagari;
  Tmp(int N)
    : N(N)
    , S(N)
    , dp(N+1)
    , kuriagari(N+1)
  {}

  ll operator() () {

    ll ret{};
    for (int i = N; i >= 0; --i) {
      auto c = i > 0 ? (S[i-1] - '0') : 0;
      auto kuri = kuriagari[i];
      c += kuri;
      if (c == 10) {
        c = 0;
        kuriagari[i-1] = 1;
      }
      assert(c < 10);
      auto cost1 = c; // 払う
      auto cost2 = (10 - c) + 1; // 繰り上げさせておつりもらう
      if (cost1 <= cost2) {
        // 払う
        if (c == 5) {
          if (i > 0
          && (S[i-2] - '0' + kuriagari[i-1]) >= 5) {
            ret += cost2 - 1;
            dp[i] = cost2 - 1;
            if (i > 0) {
              kuriagari[i-1] = 1;
            }
          }
          else {
            dp[i] = cost1;
            ret += cost1;
            ;
          }
        }
        else {
          dp[i] = cost1;
          ret += cost1;
          ;
        }
      }
      else {
        // おつりもらう
        ret += cost2 - 1;
        dp[i] = cost2 - 1;
        if (i > 0) {
          kuriagari[i-1] = 1;
        }
        else {
          abort();
        }
      }
    }
    return ret;
  }
};

void solve(void)
{
  string s; cin >> s;
  Tmp t((int)s.size());
  boost::copy(s, t.S.begin());
  auto ans = t();
  // for (const auto& s : t.dp) {
  //   cout << s;
  // }
  // cout << CRLF;
  cout << ans << CRLF;
  return;
}

int main(void)
{
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);

  solve();

  return 0;
}


Measured memory_bytes: 10140