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

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