#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define B 500
#define inf 0x3f3f3f3f
char s[55];
int a[55],len,res;
int f[55][1003][1003];

signed main()
{
	cin>>s+1,len=strlen(s+1)+3;
	For(i,1,len-3)a[i+3]=s[i]-'0';
	memset(f,63,sizeof f),f[0][B][B]=0,res=inf;
	For(i,1,len)
		For(j,-B,B)
			For(k,-i*6,i*6)
			{
				if(f[i-1][j+B][k+B]==inf)continue;
				For(p,-6,6)
				{
					int x=j*10+k+p-a[i];
					if(x>=-B&&x<=B)f[i][x+B][k+p+B]=min(f[i][x+B][k+p+B],f[i-1][j+B][k+B]+abs(p)*(len-i+1));
				}
			}
	For(i,0,2*B)res=min(res,f[len][B][i]);
	cout<<res;
	return 0;
}