#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <queue>
#include <iomanip>
#include <numeric>
#include <cmath>
using namespace std;

int main() {
	string s;
	cin >> s;
	unordered_map<int, unordered_map<int, int>> um1, um2;
	um1[0][0] = 0;
	s = "0" + s;
	for (int i = 0; i < s.size(); i++) {
		int d = s[i] - '0';
		int one = s.size() - i;
		um2.clear();
		// cout << "======" << i << "======" << endl;
		for (auto p1 : um1) {
			d = s[i] - '0';
			for (auto p2 : p1.second) {
				int left = p1.first;
				int flip = p2.first;
				int r = p2.second;
				d = s[i] - '0';
				d = d - left - flip;
				
				// cout << left << " " << flip << " " << r << " " << d << endl;
				for (int i = -25; i <= 25; i++) {
					if (um2[i * 10].count(flip + (d + i)) == 0 
						or um2[i * 10][flip + i + d] > r + abs(d + i) * one) {
						um2[i * 10][flip + (d + i)] = r + abs(d + i) * one;
					}
				}
			}
		}
		um1 = um2;
	}
	int res = -1;
	for (auto p1 : um1) {
		if (p1.first != 0) continue;
		for (auto p2 : p1.second) {
			if (res == -1 or res > p2.second) res = p2.second;
		}
	}
	cout << res << endl;
}




