#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e2+5;

#define endl '\n'

ll num;
int cnt[5];

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cout.setf(ios::fixed),cout.precision(3);
	cin>>num;
	while(num){
		int bit=num%10;
		cnt[bit%3]++;
		num/=10;
	}
	int ans;
	if(cnt[0]>0||cnt[1]>=3||cnt[2]>=3||(cnt[1]>=1&&cnt[2]>=1)){
		int tp1=cnt[1],tp2=cnt[2];
		int minn=min(cnt[1],cnt[2]);
		cnt[1]-=minn;
		cnt[2]-=minn;
		cnt[1]%=3;
		cnt[2]%=3;
		ans=0;
		if(cnt[1]) ans=cnt[1];
		if(cnt[2]) ans=cnt[2];
		cnt[1]=tp1;
		cnt[2]=tp2;
		cnt[1]%=3;
		cnt[2]%=3;
		minn=min(cnt[1],cnt[2]);
		cnt[1]-=minn;
		cnt[2]-=minn;
		if(cnt[1]==0&&cnt[2]==0) ans=0;
		else ans=min(ans,max(cnt[1],cnt[2]));
	}
	else ans=-1;
	cout<<ans<<endl;
	return 0;
}
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

///--->>> CONTAINERS <<<---///
using i64 = int64_t;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vector< ll > >;
using vpi = vector<pair<ll,ll> >;
using pi = pair<ll,ll>;
using st = set<ll>;
using stk = stack<ll>;
using kiwi = queue<ll>;
using pkiwi = priority_queue<ll>;
using mp = map<ll,ll>;
using ump = unordered_map<ll,ll>;

///--->>> TEMPLATES <<<---///
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update >;

///--->>> MACROS<<<---///
const ll mx = 3e5+123;
#define MOD 998244353
#define mod 1000000007
#define isb(z) __builtin_popcount(z)
#define lisb(z) __builtin_popcountll(z)
#define iclz(z) __builtin_clz(z)
#define lclz(z) __builtin_clzll(z)
#define ictz(z) __builtin_ctz(z)
#define lctz(z) __builtin_ctzll(z)
#define REP(i,n) for (ll i=1;i<=n;++i)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

///--->>>OPTIMIZE<<<---///
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

///--->>> DEBUGGER<<<---///
#define dbg(args...) do {cerr<<#args<<" : "; diraf(args); } while(0)
void diraf ()  {        cerr << endl;}
template < typename T, typename ... hello>void diraf( T arg, const hello &... rest) {cerr<<arg<< ' ';diraf(rest...);}

///--->>> RANDOMIZER<<<---///
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/* usage - just do rng() */

int main(int argc, char* argv[]){
    fastio;
    cin.exceptions(cin.failbit);
    ll n, tmp, cnt = 0; cin>>n;
    tmp = n;
    ll digits = floor(log10(n)) + 1;
    if(n%3==0){
        cout<<0<<endl;
        return 0;
    }
    vi dig(digits);
    pkiwi subSumDig;
    for(auto& x:dig)
        x = tmp % 10, tmp/=10;
    ll subSet = 1<<digits;

    for(ll i = 1; i<subSet; ++i){
        ll sum = 0, cnt = 0;
        for(ll j = 0; j<digits; ++j){
            if(i & (1<<j))
                sum+=dig[j], cnt++;
        }
        if(sum%3==0)
        subSumDig.emplace(cnt);
       /// dbg(i, sum);
    }

    if(subSumDig.empty()){
        cout<<-1<<endl;
        return 0;
    }
    cout<<digits - subSumDig.top()<<endl;

return 0;
}
