#include<bits/stdc++.h>
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	string s;
	cin >> s;
	int len = s.length();
	vector<int> a(len);
	for (int i = 0; i < len; i++)
		a[i] = s[len-1-i] - '0';
	a.push_back(0);
	int cnt = 0;
	for (int i = 0; i < len; i++) {
		a[i+1] += a[i]/10;
		a[i] %= 10;
		if (a[i] < 5 || (a[i] == 5 && a[i+1] < 5)) cnt += a[i];
		else {
			a[i+1]++;
			cnt += (10-a[i]);
		}
	}
	cnt += a[len];
	cout << cnt << "\n";
	return 0;
}