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

int main(){
  string S; cin >> S;

  vector<vector<int>> dp(1000010, vector<int>(2,0));

  dp[0][0] = 0; dp[0][1] = 11;

  for(int i=1; i<=S.length(); i++){
    int d = S[i-1] - '0';
    dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + d;
    dp[i][1] = min(dp[i-1][0] + 1 + 10-d, dp[i-1][1] - 1 +10-d);
  }

  cout << min(dp[S.length()][0], dp[S.length()][1]) << endl;
}