#include<bits/stdc++.h>
using namespace std;
const int N=1e6+6;

char n[N];
int f[N],g[N];

int main()
{
	int i;
	scanf("%s",n+1);
	f[0]=0;g[0]=1;
	for(i=1;n[i]!='\0';i++)
		f[i]=min(f[i-1]+n[i]-'0',g[i-1]+'9'+1-n[i]),
		g[i]=min(f[i-1]+n[i]-'0'+1,g[i-1]+'9'-n[i]);
	printf("%d",f[i-1]);
	return 0;
}