#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
const int A=200,B=330,INF=0x3f3f3f3f;
void Min(int &x,int y){x=min(x,y);}
int n,dp[55][A*2+5][B*2+5];
//111111*6=1111111-111111*4-1
//1*6=11-1*5
string s;
int main(){
	cin>>s;
	s="0000"+s;
	memset(dp,INF,sizeof dp);
	dp[0][A][B]=0;
	n=s.length();
	for(int i=0;i<n;++i){
		for(int j=-A;j<=A;++j){
			for(int k=-6*i;k<=6*i;++k){
				if(dp[i][j+A][k+B]==INF)continue;
				//dbg1(i),dbg1(j),dbg1(k),dbg2(dp[i][j+A][k+B]);
				for(int l=-6;l<=6;++l){
					int nk=k+l;
					int nj=j*10+nk-(s[i]-'0');
					if(nj<-A || nj>A)continue;
					Min(dp[i+1][nj+A][nk+B],dp[i][j+A][k+B]+(n-i)*abs(l));
				}
			}
		}
	}
	int ans=INF;
	for(int k=-6*n;k<=6*n;++k){
		ans=min(ans,dp[n][A][k+B]);
	}
	printf("%d\n",ans);
	return 0;
} 
