#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

#define MOD 1'000'000'007

#define ll long long int
#define ull unsigned ll

#define vt vector
#define uset unordered_set
#define umap unordered_map
#define mset multiset
#define umset unordered_multiset

#define in insert
#define pb push_back
#define ff first
#define ss second

#define F(i, end) LF(i, 0, end)
#define LF(i, beg, end) for (ll i = beg; i < end; ++i)
#define LR(i, end, beg) for (ll i = end; i >= beg; --i)

#define HAS(col, e) (col.find(e) != col.end())

#define outcol(col, sep) for (auto e: col) cout << e << sep

void solve()
{
    ull n;
	cin >> n;

	ll dig[3] = {0};
	ll digCount = 0;

	ll temp = n;
	while (temp)
	{
		++digCount;
		++dig[(temp % 10) % 3];
		temp /= 10;
	}

	// cout << dig[0] << ' ' << dig[1] << ' ' << dig[2] << endl;

	if (abs(dig[1] - dig[2]) == digCount)
		cout << -1 << endl;
	else
	{
		if (dig[1] % 3 == 2 && dig[2] % 3 == 0)
		{
			if (dig[2] > 0)
				cout << 1 << endl;
			else
				cout << 2 << endl;
		}
		else if (dig[1] % 3 == 0 && dig[2] % 3 == 2)
		{
			if (dig[1] > 0)
				cout << 1 << endl;
			else
				cout << 2 << endl;
		}
		else
			cout << abs((dig[1] % 3) - (dig[2] % 3)) << endl;
	}
}

int main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    cout.tie(NULL);
    
    ll c = 1;

    // cin >> c;

    F(i, c)
    {
        // cout << "Case #" << i + 1 << ": ";
        solve();
    }
    
	return 0;
}

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> II;
typedef pair<LL, II> III;
typedef priority_queue<III, vector<III>, greater<>> Queue;
static const LL INF = 1LL << 60;

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }

#ifdef __GNUC__
#define popcount __builtin_popcount
#elif _MSC_VER >= 1400
#include <intrin.h>
#define popcount __popcnt
#else
static inline int popcount(unsigned int b) {
	b = (b & 0x55555555) + (b >> 1 & 0x55555555);
	b = (b & 0x33333333) + (b >> 2 & 0x33333333);
	b = (b & 0x0F0F0F0F) + (b >> 4 & 0x0F0F0F0F);
	b += b >> 8;
	return (b + (b >> 16)) & 0x3F;
}
#endif



void solve(string N) {
	const int INF = 1 << 30;
	int ans = INF;
	int len = N.length();
	int bm = 1 << len;
	for (int b = 1; b < bm; ++b) {
		int sum = 0;
		for (int i = 0; i < len; ++i) {
			if (b & (1 << i)) {
				sum += N[i] - '0';
			}
		}
		if ((sum % 3) == 0) {
			ans = min(ans, (int)(len - popcount(b)));
		}
	}
	if (ans == INF) ans = -1;
	cout << ans << endl;
}

int main() {
	string N;
	std::cin >> N;
	solve(N);
	return 0;
}
