#include <cstdio>
#include <algorithm>

long long coins[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1};
int main()
{
	char s[1000010];
	scanf("%s", s);
	long long a = coins[s[0] - '0'];
	long long b = coins[s[0] - '0' + 1];
	for (char* p = s + 1; *p; ++p)
	{
		long long a_, b_;
		const int d = *p - '0';
		a_ = std::min(a + coins[d], b + coins[10 - d]);
		b_ = std::min(a + coins[d + 1], b + coins[9 - d]);
		a = a_;
		b = b_;
	}
	printf("%lld\n", a);
	return 0;
}
