/*  CREATED BY
    STREAM_CIPHER
    nov-2020
*/
#include<bits/stdc++.h>
using namespace std;
void __print(long long x) {cerr << x;}void __print(unsigned long long x) {cerr << x;}void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long int
#define double long double
#define fix_precision(n) cout<<fixed<<setprecision(n)
#define all(a) a.begin(),a.end()
const double pi=acos(-1.0);
int inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int mx=5*1000000;//5*64M bit ->5*8M byte ->40MB size for long long int (64 bit)
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
    #endif
        int n;
        cin>>n;
        vector<int>a;
        int nn=n;
        if(n%3==0)
        {
        	cout<<"0\n";
        	return 0;
        }
        int sum=0;
        while(nn>0)
        {
        	a.push_back(nn%10);
        	sum+=nn%10;
        	nn/=10;
        }
        int rem_1=0,rem_2=0,rem_0=0;
        for(auto i:a)
        {
        	if(i%3==1)
        		rem_1++;
        	if(i%3==2)
        		rem_2++;
        	if(i%3==0)
        		rem_0++;
        }
        if(sum%3==1)
        {
        	if(rem_1>0)
        	{
        		rem_1--;
        		if(rem_1+rem_2+rem_0>0)
        			cout<<"1\n";
        		else
        			cout<<"-1\n";
        	}
        	else
        	{
        		if(rem_0>0)
        		{
        			cout<<rem_2<<endl;
        		}
        		else
        			cout<<"-1\n";
        	}
        }
        if(sum%3==2)
        {
        	if(rem_2>0)
        	{
        		if(rem_2-1+rem_1+rem_0>0)
        			cout<<"1\n";
        		else
        		{
        			if(rem_1>1)
        			{
        				rem_1-=2;
        				if(rem_0+rem_1+rem_2>0)
        					cout<<"2\n";
        				else
        					cout<<"-1\n";
        			}
        			else
        			{
        				if(rem_0>0)
        					cout<<rem_1+rem_2<<endl;
        				else
        					cout<<"-1\n";
        			}
        		}
        	}
        	else
        	{
        		if(rem_1>1)
    			{
    				rem_1-=2;
    				if(rem_0+rem_1+rem_2>0)
    					cout<<"2\n";
    				else
    				{
    					if(rem_0>0)
        					cout<<rem_1+rem_2<<endl;
        				else
        					cout<<"-1\n";
    				}
    			}
    			else
    			{
    				if(rem_0>0)
        				cout<<rem_1+rem_2<<endl;
    				else
    					cout<<"-1\n";
    			}
        	}
        }

        
}
//always use s.lower_bound instead of lower_bound(all(s))
//also whenever there exist an standerd method for stl container prefer that
//otherwise this will happen
//https://codeforces.com/contest/1435/submission/96683823//tle
//https://codeforces.com/contest/1435/submission/96703923//accepted
#include<iostream>
#include<cstdio>
using namespace std;
int a[5]={0};
int main()
{
	char ch;
	int mid=0;
	while(scanf("%c",&ch)!=EOF)
	{
		if(ch>'9'||ch<='0')
		  break;
		a[(ch-'0')%3]++;
		mid+=(ch-'0')%3;
	}
	mid%=3;
	if(!mid)
	{
		cout<<0;
		return 0;
	}
	if(a[mid]>1||(a[mid]==1&&a[0]+a[3-mid]))
	{
		cout<<1;
		return 0;
	}
	if(mid==2&&(a[1]>2||(a[1]==2)&&a[0]+a[2]))
	{
		cout<<2;
		return 0;
	}
	if(mid==1&&(a[2]>2||(a[2]==2&&a[0]+a[1])))
	{
		cout<<2;
		return 0;
	}
	cout<<-1;
	return 0;
}