#include <iostream>
#include <cstdio>
#define N 1000005
using namespace std;

int s, d[2][N];
string a;
int main()
{
	int i;
	cin >> a;
	a = '0' + a;
	d[1][0] = 1;
	for (i = 1; i < a.size(); i++) {
		a[i] -= '0';
		d[0][i] = min(d[0][i - 1] + a[i], d[1][i - 1] + 10 - a[i]);
		d[1][i] = min(d[0][i - 1] + a[i] + 1, d[1][i - 1] + 9 - a[i]);
	}
    cout << d[0][a.size() - 1] << endl;
    return 0;
}
