#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vi vector <int>
#define vp vector <pair<int,int>>
#define f(x,y,z) for(x=y;x<z;x++)
#define pb push_back
#define mp make_pair
#define ld long double
#define fb(x,y,z) for(x=y;x>z;x--)
#define ps(x,y) fixed << setprecision(y) << x
#define PI 3.1415926535
#define mod 1000000007 
#define mods 998244353 
#define setbits(x) __builtin_popcountll(x)
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a,b,sizeof(a))


// AUTHOR ALOK KUMAR
int lcm(int a,int b)
{
    return (a*b)/(__gcd(a,b));
}
int power(int a,int b)
{
    int c=1;
    while(b)
    {
        if(b&1)
        c*=a;
        a*=a;
        b>>=1;
    }
    return c;
}

void solve()
{
    int n;
    cin>>n;
    int a[3];
    int sum=0;
    fill(a,0);
    int count=0;
    while(n)
    {
        int i,j;
        j=n%10;
        a[j%3]++;
        n/=10;
        sum+=j;
        count++;
    }
    if(sum%3==0)
    cout<<"0\n";
    else if(sum%3==1)
    {
        if(a[1]&&count>1)
        cout<<"1\n";
        else
        {
            if(a[2]>=2&&count>2)
            cout<<"2\n";
            else
            cout<<"-1\n";
        }
    }
    else
    {
        if(a[2]&&count>1)
        cout<<"1\n";
        else
        {
            if(a[1]>=2&&count>2)
            cout<<"2\n";
            else
            cout<<"-1\n";
        }
    }
    
    
    
}



int32_t main()
{
    IOS
    int t=1;
    //cin>>t;
    while(t--)
    solve();
    return 0;
}





































#include <bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T; cin >> T; 
	string U = "atcoder"; 
	while(T--){
		string S; cin >> S; 
		int N = S.size();
		bool alla = true; 
		for(int i = 0; i < N; i++){
			if(S.at(i) != 'a') alla = false; 
		}
		if(alla){
			cout << -1 << "\n"; continue; 
		}
		if(S > U){
			cout << 0 << "\n"; continue; 
		} 
		int ans = 1919; 
		for(int i = 1; i < N; i++){
			string V = S; 
			int cnt = 0; 
			for(int j = i; j >= 1; j--){
				swap(V.at(j-1), V.at(j)); 
				cnt++; 
				if(V > U){
					ans = min(ans, cnt); 
					break; 
				}
			}
		}
		cout << ans << "\n"; 
	}
}