#include <cstring>
#include <iostream>
using namespace std;
int main() {
	string s;
	cin >> s;
	s = "0" + s;
	int n = s.size(), ans = 0;
	for (int i = n - 1; i >= 0; i--) {
		if (s[i] <= '4' or (s[i] == '5' and s[i - 1] < '5'))
			ans += s[i] - '0';
		else {
			s[i - 1]++;
			ans += ('9' - s[i]) + 1;
		}
	}
	printf("%d\n", ans);
	return 0;
}