#include <bits/stdc++.h>
using namespace std;
int sum[66666];
int kk=2,a=10,b=1;
int minx=0,minxx=0;

int main()
{
	for(int i=1;i<=10000;i++)  sum[i]=i;
	long long k;
	cin>>k;
	long long k2=k,ni,n,m;
	while(ni!=0)
	{
		ni/=10;
		int t=k%a/b;
		a*=10;b*=10;
		minx+=t;
	}
	if(minx==1)
	{
	cout<<minx<<endl;
	return 0;
	}
	a=10;b=1;
	n=k;
		while(kk!=1000)
	{
		n/=10;
		m=k2%a/b;
		a*=10;b*=10;
		minxx+=m;
		if(minxx>minx) {n=k*sum[kk++];a=10;b=1;k2=n;minxx=0;continue;}
		if(m>minx) {n=k*sum[kk++];a=10;b=1;k2=n;minxx=0;continue;}
		if(minx==1) break;
		if(n==0&&minxx<minx) {minx=minxx;n=k*sum[kk++];a=10;b=1;k2=n;minxx=0;continue;}
		if(n==0) {n=k*sum[kk++];a=10;b=1;k2=n;minxx=0;continue;}
		
	}
	
	

	
	cout<<minx<<endl;
	
	return 0;
}
