#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion
#pragma region chminmax
template <typename T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion
using vvll = vector<vll>;
const ll INF = 1e12;
int main()
{
    string s;
    cin >> s;
    int n = s.size();

    vvll dp(n + 1, vll(2, INF));
    dp[0][0] = 0;
    dp[0][1] = 1;

    rep(i, n)
    {
        int x = s[i] - '0';
        chmin(dp[i + 1][0], dp[i][0] + x);
        chmin(dp[i + 1][1], dp[i][0] + x + 1);
        chmin(dp[i + 1][0], dp[i][1] + (10 - x));
        chmin(dp[i + 1][1], dp[i][1] + (10 - x) - 1);
    }

    cout << dp[n][0] << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
 
template <typename T> void print(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void print(T t, Args... args) { cout<<t<<" "; print(args...); }

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

int n;
string s;
int cache[1000004][3];
int dp(int pos,int carr)
{
    if(pos==n)
        return carr;
    int &ans=cache[pos][carr];
    if(ans!=-1)
        return ans;
    ans=1e18;
    for(int i=0;i<10;i++)
    {
        int dig1=i;
        int dig2=(i+carr+s[pos]-'0')%10;
        ans=min(ans,dig1+dig2+dp(pos+1,(i+carr+s[pos]-'0')/10));
    }
    return ans;
}
int32_t main() 
{
    cin>>s;
    reverse(s.begin(),s.end());
    n=s.length();
    memset(cache,-1,sizeof(cache));
    print(dp(0,0));
}   
