#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6+7;
int a[maxn],n;
int f[maxn][2];

int main()
{
	char s=getchar();
	while(s<'0'||s>'9') s=getchar();
	while(s>='0'&&s<='9') a[++n]=s-'0',s=getchar();
	for(int i=n;i>=0;--i) {
		for(int j=0;j<=9;++j)
			f[i][0]=min(f[i+1][0]+a[i+1],f[i+1][1]+10-a[i+1]);
			f[i][1]=min(f[i+1][0]+a[i+1]+1,f[i+1][1]-1+10-a[i+1]);
	}
	printf("%d",f[0][0]);
}