#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;

int main()
{
    string N;
    cin >> N;
    int k = N.length();
    vector<int> v(10, 0);
    REP(i, k)
    {
        v.at(N.at(i) - '0')++;
    }

    vector<int> m(3, 0); //mod 3
    int sum = 0;
    REP(i, 10)
    {
        m.at(i % 3) += v.at(i);
        sum += i * v.at(i);
    }

    int mod = sum % 3;

    if (mod == 0)
    {
        cout << 0 << endl;
    }
    else if (mod == 1)
    {
        if (k >= 2 && m.at(1) >= 1)
        {
            cout << 1 << endl;
        }
        else if (k >= 3 && m.at(2) >= 2)
        {
            cout << 2 << endl;
        }
        else
        {
            cout << -1 << endl;
        }
    }
    else if (mod == 2)
    {
        if (k >= 2 && m.at(2) >= 1)
        {
            cout << 1 << endl;
        }
        else if (k >= 3 && m.at(1) >= 2)
        {
            cout << 2 << endl;
        }
        else
        {
            cout << -1 << endl;
        }
    }else{
        cout << -1 << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007

const ll mod = 1e9 + 7;

ll power(ll a, ll b) { //a^b
	ll res = 1;
	a = a % MOD;
	while (b > 0) {
		if (b & 1) {res = (res * a) % MOD; b--;}
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	string s;
	cin>>s;
	ll n=s.length();
	ll ans=0;
	ll a;
	ll b;
	ll c;
	a=b=c=0;
	for(ll i=0;i<n;i++){
		ll cur=s[i]-'0';
		ans=(ans+cur)%3;
		if(cur%3==0)
			a++;
		else if(cur%3==1)
			b++;
		else
			c++;
	}
	if(ans==0)
		cout<<"0\n";
	else{
		if(ans==1){
			if(b>0 && n>1)
				cout<<"1\n";
			else if(c>1 && n>2)
				cout<<"2\n";
			else
				cout<<"-1\n";
		}
		else{
			if(c>0 && n>1)
				cout<<"1\n";
			else if(b>1 && n>2)
				cout<<"2\n";
			else
				cout<<"-1\n";
		}
	}
}
