#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(),V.end()

ll mc[2][1000010];

int main() {
	string S;
	cin >> S;
	int ss = S.size();
	mc[0][ss] = 0;
	mc[1][ss] = INT32_MAX;
	for (int i = ss - 1;i >= 0;i--) {
		mc[0][i] = min(mc[0][i + 1] + (ll)(S[i] - 48), mc[1][i + 1] + (ll)(S[i] - 48) + 1);

		mc[1][i] = min(mc[0][i + 1] + 10 - (ll)(S[i] - 48), mc[1][i + 1] + 9 - (ll)(S[i] - 48));
	}
	cout << min(mc[0][0], mc[1][0] + 1) << endl;
}