#include <bits/stdc++.h>

using namespace std;

constexpr int MAXLN = 1000000;

char N[MAXLN + 2];

int main() {
	cin >> (N + 1);
	N[0] = '0';
	int n = strlen(N);
	int dpa = 0, dpb = INT_MAX - 1;
	for(int i = n - 1; i >= 0; i--) {
		N[i] -= '0';
		int ndpa = N[i] + min(dpa, dpb + 1);
		int ndpb = 10 - N[i] + min(dpa, dpb - 1);
		dpa = ndpa; dpb = ndpb;
	}
	cout << dpa << '\n';
}
