#pragma warning(disable:4996)
#include <bits/stdc++.h> 
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	ll n;
	while (cin >> n)
	{
		int a[18] = { 0 };
		int top = -1, sum = 0;
		int ans = INT_MAX;
		bool flag = true;
		ll tempn = n;
		while (tempn) {
			a[++top] = tempn % 10;
			sum += tempn % 10;
			tempn /= 10;
		}
		for (int i = 0; i <= (1 << 18); i += 1) {
			tempn = n;
			int state = i, pos = 0, num = 0;
			while (state) {
				if (state & 1) tempn -= a[pos], ++num;
				++pos;
				state >>= 1;
			}
			if (num > top) break;
			if (tempn % 3 == 0) ans = min(ans, num), flag = false;
		}
		if (flag) cout << "-1" << endl;
		else cout << ans << endl;
	}
	return 0;
}
#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()
#define lc(i) 2*i
#define rc(i) 2*i+1
//#define int long long
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vii = vector<ii>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 5;

signed main() {
    FAST_IO;
#ifdef arujbansal_local
    setIO("input.txt", "output.txt");
#endif

    string s;
    cin >> s;

    int n = sz(s);

    vi a(n);
    for (int i = 0; i < n; i++) a[i] = s[i] - '0';

    int mn = n;

    for (int mask = 1; mask < (1 << n); mask++) {
        int num = 0, sum = 0;

        for (int i = 0; i < n; i++) {
            if (mask & (1 << i)) {
                sum += a[n - i - 1];
                num++;
            }
        }

        if (sum % 3 == 0)
            mn = min(mn, n - num);
    }

    cout << (mn == n ? -1 : mn);
}