#include <bits/stdc++.h>
using namespace std;
int ans[2], n, t[2], len;
char s[1000005];
int main(){
	scanf("%s", s);
	len = strlen(s);
	for(int i=len-1; i>=0; i--){
		n = s[i]-'0';
		t[0] = min(ans[0],ans[1]+1) + n;
		if(i < len-1) t[1] = min(ans[0]+(10-n), ans[1]+(10-n-1));
		else t[1] = ans[0]+(10-n);
		ans[0] = t[0];
		ans[1] = t[1];
	}
	printf("%d\n", min(ans[0],ans[1]+1));
}