#include <bits/stdc++.h>
#include <string>
#include <cmath>
#include <sstream>
//#include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for(int i = (a-1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
typedef long long int ll;
using namespace std;
ll mod=1000000007;
//ll mod=67280421310721;
//ll mod=998244353;
ll INF=1e18;
ll po(ll a,ll b)
{
	ll re=1;
	while(b)
	{
		if(b%2==1)
		{
			re=(re*a);
			re%=mod;
		}
		a=(a*a);
		a%=mod;
		b>>=1;
	}
	return re;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
//	cin>>test;
	while(test--)
	{
		string s;
		cin>>s;
		int n=s.size();
		int ans=0;
		FORD(i,n,0)
		{
			if(s[i]=='5')
			{
				ans+=5;
				if(i!=0)
				{
					if(s[i-1]>='5')
					s[i-1]=(s[i-1]+1);
				}
			}
			else if(s[i]<'5')
			{
				ans+=(s[i]-'0');
			}
			else
			{
				ans+=(('9'-s[i])+1);
				if(i==0)
				ans++;
				else
				s[i-1]=(s[i-1]+1);
			}
		}
		cout<<ans;
	}
	return 0;
}

#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

void solve();

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

int dp[10000000+1][2];

void solve() {
  string S; cin >> S;

  reverse(S.begin(), S.end());

  dp[0][0] = S[0] - '0';
  dp[0][1] = 10 - (S[0] - '0');

  for (int i = 1; i < S.size(); i++) {
    int s = S[i] - '0';
    if (0 < s && s < 9) {
      dp[i][0] = min(dp[i-1][0] + s, dp[i-1][1] + s + 1);
      dp[i][1] = min(dp[i-1][0] + (10 - s), dp[i-1][1] + (10 - (s + 1)));
    } else if (s == 0) {
      dp[i][0] = min(dp[i-1][0], dp[i-1][1] + 1);
      dp[i][1] = dp[i-1][1] + 9;
    } else if (s == 9) {
      dp[i][0] = dp[i-1][0] + 9;
      dp[i][1] = min(dp[i-1][0] + 1, dp[i-1][1]);
    }
  }

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