#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 55, J = 30, K = 155, inf = 1e7;

void cmin(int &x, int y) {if(x > y) x = y;}
void cmax(int &x, int y) {if(x < y) x = y;}

int n, f[N][J * 2 + 1][K * 2 + 1]; //f[i][j][k] 表示 i+1为还需要增量j 第0位受到增量k 时的最小花费。 
char a[N];

inline void upd(int i, int j, int k, int v) {
	if(-J <= j && j <= J && -K <= k && k <= K) 
		cmin(f[i][j + J][k + K], v);
}

int main() {
	scanf("%s", a);
	for(n = 0;a[n]; n++) a[n] -= '0';
	reverse(a, a + n);
	memset(f, 1, sizeof f);
	f[n][J][K + a[0]] = 0;
	REP(i, 1, n) {
		int x = a[i];
		rep(j, -J, J) rep(k, -K, K) if(f[i][j + J][k + K] < inf) 
			rep(d, -3, 3) upd(i - 1, -x + d * 10, k + d, f[i][j + J][k + K] + (i + 1) * abs(j + x - d));
	}
	int ans = inf;
	rep(j, -J, J) rep(k, -K, K) 
		cmin(ans, f[0][j + J][k + K] + abs(j + k));
	printf("%d\n", ans); return 0;
}

