#include<iostream>
using namespace std;
int main(){
	string s;
  cin>>s;
  long long a=0,b=0,carry=0;
  for(int i=s.length()-1;i>=0;i--){
  	int p=s[i]-'0'+carry;
    carry=0;
    if(p>5){
    	b+=10-p;
  		carry=1;
    }
    else if(p==5){
    	if(s[i-1]-'0'>=5){
			b+=p;
			carry=1;
		}
		else{
			a+=p;
		}
	}
    else{
      a+=p;
    }
  }
  long long ans=a+b+carry;
  cout<<ans<<endl;
}