#include <iostream>
#include <cstdio>
using namespace std;

string a;
int d, cr, ans;
int main() {
	cin >> a;
	a = '0' + a;
	for(int i=a.size()-1; i>0; i--) {
		d = a[i] - '0' + cr;
		ans += min(d, 10-d);
		cr = (d>5 || d==5 && a[i-1]>='5');
	}
	cout << ans + cr;
	return 0;
}