#include<bits/stdc++.h>
using namespace std;
long long n;
struct node
{
	int sum;
	bool p[30];
	int jian;
};
int main()
{
	cin>>n;
	long long m=n;
		int  len=0;
		int s[25]; 
		while(m>0)
		{
			s[++len]=m%10;
			m/=10;
		}
	queue<node>q;
	node node1;
	node1.sum=0;
	for(int i=1;i<=len;i++)
	{
	node1.sum+=s[i];
	node1.p[i]=false;	
	}
	node1.jian=0;
	q.push(node1);
	while(q.size())
	{
		node node2=q.front();
		q.pop();
		if( node2.sum!=0 && node2.sum%3==0) 
		{
			cout<<node2.jian<<endl;
			return 0;
		}
		for(int i=1;i<=len;i++)
		{
			 
			 if(node2.p[i]==true) continue;
			 node node3;
			 node3.jian=node2.jian+1;
			 node3.sum=node2.sum-s[i];
			 memcpy(node3.p,node2.p,sizeof node2.p);
			 node3.p[i]=true;
			 q.push(node3);
		}
	}
	cout<<-1<<endl;
}
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i,a,n) for(ll i=a;i<(ll)(n);i++)

int main() {
    ll a; cin>>a;
    string s = to_string(a);
    ll sum = 0;

    if(a%3 == 0){
        cout << 0 << endl;
        return 0;
    }

    rep(i, s.size()){
        sum += s[i] - '0';
    }

    ll n = s.size();
    ll ans = n;
    for (ll bit = 0; bit < (1<<n); ++bit) {
        ll ss = sum;
        ll count = 0;
        for (ll i = 0; i < n; ++i) {
            if (bit & (1<<i)) {
                ss -= s[i]-'0';
                count++;
            }
        }
        if(ss != 0 && ss%3 == 0 && count != n){
            ans = min(ans, count);
        }
    }
    cout << (ans == n ? -1 : ans) << endl;
}