#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>
#include<iomanip>
#include<float.h>
 
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mp(a, b) make_pair(a, b)
#define mt(a,b,c) make_tuple(a,b,c)
#define rank qwertyuio
#define next dfghjk
#define plus fsghsf
#define minus ytryr
#define pb push_back
#define debug(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
 
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
 
const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int mod = (int)1e9 + 7;
const int mod1 = (int)1e9 + 9;
const int mod2 = 998244353;
const int mod3 = (int)1e6+3;
const ld eps = 1e-12;
const double pi = atan2(0, -1);
const int ABC = 26;
 void lineFromPoints(pdd P, pdd Q, double &a, 
                        double &b, double &c) 
{ 
    a = Q.second - P.second; 
    b = P.first - Q.first; 
    c = a*(P.first)+ b*(P.second); 
} 
  
// Function which converts the input line to its 
// perpendicular bisector. It also inputs the points 
// whose mid-point lies on the bisector 
void perpendicularBisectorFromLine(pdd P, pdd Q, 
                 double &a, double &b, double &c) 
{ 
    pdd mid_point = make_pair((P.first + Q.first)/2, 
                            (P.second + Q.second)/2); 
  
    // c = -bx + ay 
    c = -b*(mid_point.first) + a*(mid_point.second); 
  
    double temp = a; 
    a = -b; 
    b = temp; 
} 
  
// Returns the intersection point of two lines 
pdd lineLineIntersection(double a1, double b1, double c1, 
                         double a2, double b2, double c2) 
{ 
    double determinant = a1*b2 - a2*b1; 
    if (determinant == 0) 
    { 
        // The lines are parallel. This is simplified 
        // by returning a pair of FLT_MAX 
        return make_pair(FLT_MAX, FLT_MAX); 
    } 
  
    else
    { 
        double x = (b2*c1 - b1*c2)/determinant; 
        double y = (a1*c2 - a2*c1)/determinant; 
        return make_pair(x, y); 
    } 
} 
  
pdd findCircumCenter(pdd P, pdd Q, pdd R,pdd S) 
{ 
    // Line PQ is represented as ax + by = c 
    double a, b, c; 
    lineFromPoints(P, Q, a, b, c); 
  
    // Line QR is represented as ex + fy = g 
    double e, f, g; 
    lineFromPoints(R, S, e, f, g); 
  
    // Converting lines PQ and QR to perpendicular 
    // vbisectors. After this, L = ax + by = c 
    // M = ex + fy = g 
    perpendicularBisectorFromLine(P, Q, a, b, c); 
    perpendicularBisectorFromLine(R, S, e, f, g); 
  
    // The point of intersection of L and M gives 
    // the circumcenter 
    pdd circumcenter = 
           lineLineIntersection(a, b, c, e, f, g); 
  
    
    return circumcenter;
} 

double dis(double x1,double x2,double y1,double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    string n;
    cin>>n;
    ;
    int len=n.length();
    string m(len,'0');
    ll dp[len];
    ll swap[len];
    int i;
    ll ans=0;
    int tod=0;
    repb(i,len-1,-1){
        int temp=n[i]-'0';
        if(temp==0&&tod==0){
            m[i]='0';
        }
        else if(temp==0&&tod==1){
            m[i]='9';
            tod=1;
        }
        else if(tod==0){
            tod=1;
            m[i]=10-temp+'0';
        }
        else{
            m[i]=9-temp+'0';
        }
    }
    //cout<<m<<endl;
    dp[len-1]=min(n[len-1]-'0',11-n[len-1]+'0');
    swap[len-1]=min(m[len-1]-'0',11-m[len-1]+'0');
    repb(i,len-2,-1){
        int temp=n[i]-'0';
        int temp2=min(temp,11-temp);
        int temp3=m[i]-'0';
        int temp4=min(temp3,11-temp3);
        dp[i]=min(swap[i+1]+temp2+1,dp[i+1]+temp2);
        swap[i]=min(swap[i+1]+temp4,dp[i+1]+temp4+1);
        //cout<<dp[i]<<" "<<swap[i]<<endl;

    }
    
    ans=min(swap[0]+1,dp[0]);
    cout<<ans;
    
             
    
}

Measured memory_bytes: 17948