#include<bits/stdc++.h>
using namespace std;
string s;int ans;
int main()
{
	cin>>s;s='0'+s;
	reverse(s.begin(),s.end());
	for(int i=0;i<s.size();i++)
	{
		if(s[i]-'0'>5||(s[i]=='5'&&s[i+1]>='5'))
		{
			s[i]=(10-(s[i]-'0')+'0');
			s[i+1]++;
		}
	}
	for(int i=0;i<s.size();i++)
	{
		ans+=s[i]-'0';
	}
	cout<<ans;
	return 0;
}