#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <algorithm>
#define rep(i, n) for(i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int main(void) {
	int num, i, k = 0, ans = 0;
	string s;
	cin >> s;
	vector<vector<ll>> dp(s.size() + 1, vector<ll>(2, 0));
	dp[0][1] = 1;
	rep(i, s.size()) {
		int n = s[i] - '0';
		dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n);
		dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - n - 1);
	}
	cout << dp[s.size()][0] << "\n";
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9+7;


int main(){
  string S;
  cin >> S;
  ll ans = 0;
  int N = S.size();
  bool flag = false;
  REPR(i, N-1){
    int d = S[i] - '0';
    if(flag) d++;
    flag = false;
    if(d < 5){
      ans += d;
    }
    else if(d > 5){
      ans += 10 - d;
      flag = true;
    }
    else{
      if(i != 0 && S[i-1] >= '5'){
        ans += d;
        flag = true;
      }
      else{
        ans += d;
      }
    }
  }
  if(flag) ans++;
  cout << ans << endl;
  return 0;
}