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

const int MAX_N = (int)1e6 + 10;

int main () {
    string N; cin >> N;
    int n = N.length();
    int f[n+1][2];
    f[0][0] = 0; f[0][1] = 1;
    for (int i = 1; i <= n; i++) {
        int t = N[i-1]-'0';
        f[i][0] = min(f[i-1][0] + t, f[i-1][1] + (10-t));
        f[i][1] = min(f[i-1][0] + t + 1, f[i-1][1] + (9-t));
    }
    cout << f[n][0] << endl;
}
