# include <bits/stdc++.h>
# define LL long long
using namespace std;
const int N = 2e6 + 10;
int d[N][2];
int main(){
	string s;
	cin >> s;
	d[0][0] = s[0] - '0';
	d[0][1] = 11 - (s[0] - '0');
	for(int i = 1; i < s.size(); i++){
		d[i][0] = min(d[i - 1][0], d[i - 1][1]) + s[i] - '0';
		d[i][1] = min(d[i - 1][0] + 1, d[i - 1][1] - 1) + 10 - (s[i] - '0');
	}
	cout << min(d[s.size() - 1][0], d[s.size() - 1][1]) << endl;
}
