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

int main(void)
{
    ll N;
    cin >> N;

    int k = 0;
    vector<int> a;
    while(N > 0)
    {
        a.push_back(N%10);
        N /= 10;
        ++k;
    }

    int ans = -1;

    for (int bit = 0; bit < (1<<k); ++bit) 
    {
        int tmp = 0;
        int tmpa = 0;
        for(int i = 0; i < k; ++i)
        {
            if (bit & (1 << i)) {
                tmp += a[i];
                ++tmpa; 
            } 
        }
        if(tmp == 0) continue;
        if(tmp%3 == 0)
        {
            if(ans == -1) ans = k-tmpa;
            ans = min(k-tmpa, ans);
        }
    }


    cout << ans << endl;

    return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

int dfs(long long n, int p, int q, int r) {
    if (n == 0) {
        return p > q && r % 3 == 0 ? q : 100;
    } else {
        return min(dfs(n / 10, p + 1, q, r + n % 10), dfs(n / 10, p + 1, q + 1, r));
    }
}

int main() {
    long long n;
    cin >> n;

    int res = dfs(n, 0, 0, 0);
    if (res < 100) {
        cout << res << endl;
    } else {
        cout << "-1" << endl;
    }
}
