#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string N;
  cin >> N;
  N = "0" + N;
  vector<vector<int>> dp(N.size() + 1, vector<int>(2, 1 << 28));
  dp[0][0] = 0;
  for (int i = 0; i < (int)N.size(); i++) {
    dp[i + 1][0] = min(dp[i][0] + N[i] - '0', dp[i][1] + 10 - (N[i] - '0'));
    dp[i + 1][1] =
        min(dp[i][0] + N[i] - '0' + 1, dp[i][1] + 10 - (N[i] - '0') - 1);
  }
  cout << dp[N.size()][0] << '\n';
  return 0;
}
