//#pragma GCC oplmize(2)
#include <bits/stdc++.h>
#define ll long long
#define sc(x) scanf("%lld",&x)
#define scs(x) scanf("%s",x+1)
#define pr(x) printf("%lld\n",x)
#define prs(x) printf("%s\n",x)
using namespace std;
const int maxn=1e7+5;
const int mod=998244353;
const double pi=acos(-1.0);
const double eps = 1e-8;
char s[maxn];
int a[maxn],len;
ll sum;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    scs(s);
    len=strlen(s+1);
    for(int i=1;i<=len;i++){
        a[i]=s[len-i+1]-'0';
    }
    for(int i=1;i<=len+1;i++){
        if(a[i]==10){
            a[i]-=10;
            a[i+1]++;
        }
        if(a[i]>5){
            sum+=(10-a[i]);
            a[i+1]++;
        }
        else if(a[i]<5){
            sum+=a[i];
        }
        else{
            sum+=5;
            if(a[i+1]>=5){
                a[i+1]++;
            }
        }
    }
    pr(sum);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define name ""
#define ini freopen(name".inp","r",stdin); freopen(name".out","w",stdout)
#define foe(it,c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long 
#define db double 
#define pii pair <int, int>
#define pll pair <long, long>
#define all(c) c.begin(), c.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

const int INF = 1e9+7;
const int MOD = 1e9+7;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};

const int N = 1e6+2;
string s;
int n, dp[N][2], ssum[N];

int main()
{
  fastio;
  cin >> s; reverse(all(s)); 
  int n = s.size(); s = '#' + s;
  for (int i = n; i > 0; i--) ssum[i] = ssum[i + 1] + (9 - (s[i] - '0'));
  dp[0][0] = dp[0][1] = 0;
  int mn = ssum[1];
  for (int i = 1; i <= n; i++){
    dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + (s[i] - '0');
    dp[i][1] = mn - ssum[i + 1] + 2;
    mn = min(mn, dp[i][0] + ssum[i + 1]);
    mn = min(mn, dp[i][1] + ssum[i + 1]);
  }
  cout << min(dp[n][0], dp[n][1]); 
  return 0;
}