# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
typedef long long lint;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve() {
    string N;
    cin >> N;

    int ans = 1000;
    for(int i=1;i<(1<<N.size());i++ ){
        int tmp=0;
        int cnt = 0;
        for(int j=0;j<N.size();j++){
            if(i&(1<<j)) tmp += N[j]-'0';
            else cnt++;
        }

        if(tmp%3 == 0){
            ans = min(ans, cnt);
        }
    }
    if(ans==1000) cout << -1 << endl;
    else cout << ans << endl;
}
signed main() {

    solve();

    return 0;
}
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <bitset>
#include <set>
#include <cmath>
#define LL long long
#define ull unsigned long long
using namespace std;
const int MAXN = 25;
int n;
int ans = 25;
LL ret = 0;

LL vis[(int)3e5 + 5];

char s[MAXN];

void dfs (int dep) {
	//++ret;
	LL x = 0;
	//if (dep > n) return ;
	int q = 0;
	for (int i=0; i<n; ++i)
		if (s[i] != 'a') q |= (1 << i);
	if (vis[q]) return ;
	vis[q] = 1;
	for (register int i = 0; i < n; ++i)
		if (s[i] != 'a') x *= 10ll, x += s[i] - '0';
	//cout << x << endl;
	
	if (x - 3ll * (x / 3ll) == 0) {
		//cout << x << endl;
		ans = min (ans, dep);
		return ;
	}
	for (register int i = n-1; ~i; --i)
		if (s[i] ^ 'a') {
			char c = s[i];
			s[i] = 'a', dfs (dep + 1);
			s[i] = c;
		}
}

int main() {
#ifdef IN
	freopen ("x.in", "r", stdin);
	freopen ("x.out", "w", stdout);
#endif
	int i,j,k;
	int x;
	scanf ("%s", s), n = strlen (s);
	//cout << n << endl;
	dfs (0);
	if (ans < n) printf ("%d", ans);
	else printf ("-1");
	//cout << "\n" << ret;
	return 0;
}
