#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,string> P;
#define rep(i,m,n) for(int i=m;i<n;i++)
ll mod=1e9+7;

int main(){
  string s;
  cin>>s;
  int n=s.size();
  int dp0[n];
  int dp1[n];
  dp0[0]=min(s[0]-'0',11-(s[0]-'0'));
  dp1[0]=min(s[0]-'0'+1,10-(s[0]-'0'));
  rep(i,1,n){
    int x=s[i]-'0';
    dp0[i]=min(dp0[i-1]+x,dp1[i-1]+10-x);
    dp1[i]=min(dp0[i-1]+x+1,dp1[i-1]+9-x);
  }
  cout<<dp0[n-1]<<endl;
}