#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;


int main()
{
	string s;
	cin >> s;
	long long int n = s.length();
	long long int one = 0, two = 0, sum = 0;
	for (long long int i = 0; i < n; i++)
	{
		int num = s[i] - 48;
		if (num % 3 == 1)
			one++;
		else if (num % 3 == 2)
			two++;
		sum += num;
	}
	int res = -2;
	if (sum % 3 == 0)
		res = 0;
	else {
		if (abs(two - one) < n)
		{
			res = abs(two - one);
		}
		two = two % 3;
		one = one % 3;
		if (abs(two - one) < n)
				res = abs(two - one) > res ? res : abs(two - one);
	}

	if (res == -2)
		res = -1;

	cout << res << endl;

}
#include<bits/stdc++.h>
#define pu push
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define rrep(i,x) for(int i=1;i<=x;i++)
#define drep(i,n) for(int i = (n)-1; i >= 0; i--)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define BITSC(x,v) (binary_search(x.begin(),x.end(),v)
#define dup(x,y) (((x)+(y)-1)/(y))
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
const double eps =1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

int main(){
    string n;cin>>n;
    int ans=2000;
    srep(i,1,(1<<n.size())){
        int temp=0;
        int count=0;
        rep(j,n.size()){
            if((1<<j)&i){
                temp+=n[j];
                count++;
            }
        }
        if(temp%3==0&&ans>n.size()-count)ans=n.size()-count;
    }
    
    if(ans==2000)cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}