#include<bits/stdc++.h>
using namespace std;

const int inf=1e9;
int dp[10000010][2];

int main(){
    string s; cin >> s;
    reverse(s.begin(),s.end());
    s+='0';
    int N=s.size();
    dp[0][1]=inf;
    for(int i=0;i<N;i++){
        int d=s[i]-'0';
        dp[i+1][0]=min(dp[i][0],dp[i][1]+1)+d;
        dp[i+1][1]=min(dp[i][0]+(10-d),dp[i][1]+(10-(d+1)));
    }
    cout << dp[N][0] << endl;
}