#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main() {
  std::string str; std::cin >> str;
  std::vector<int> v, mod;
  int sum = 0;
  for (int i = 0; i < str.length(); ++i) {
    v.push_back(str[i] - '0');
    sum += v[i];
    if (v[i] % 3) mod.push_back(v[i] % 3);
  }

  std::sort(mod.begin(), mod.end());
  int ans = -1;

  if (sum % 3 == 0) ans = 0;
  else if (!mod.empty()) {
    if (sum % 3 == 1) {
      if (mod[0] == 1) {
	ans = 1;
      } else if (mod.size() > 1 && mod[mod.size() - 2] == 2) {
	ans = 2;
      }
    } else {
      if (mod[mod.size() - 1] == 2) {
	ans = 1;
      } else if (mod.size() > 1) {
	ans = 2;
      }
    }
  }

  if (ans == str.length()) ans = -1;

  std::cout << ans << std::endl;
  return 0;
}

    

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>> vec_p; //vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;//llの最大9*10^18
const int INF = pow(2, 30) - 1;

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll N;
    string S;
    cin >> S;
    vector<ll> A(S.size());
    bool flag = false;
    ll ans = 10000000000000;

    REP(i,S.size()){
        A.at(i) = S.at(i) - '0';
    }

    ll b = A.size();

    for (int bit = 0; bit <= (1 << S.size()); ++bit)
    {

        ll tmp = 0;
        ll tmp1 = 0;

        for (int i = 0; i < S.size(); ++i)
        {

            if (bit & (1 << i))
            { // 列挙に i が含まれるか
                tmp += A.at(i);
                tmp1++;
            }


        }
        if (tmp % 3 == 0 && tmp > 0)
        {
            flag = true;
            ans = min(ans, b - tmp1);
        }

        //cout << bit << " " << b - tmp1 << endl;
        tmp = 0;
        tmp1 = 0;
    }

    if(flag){
        cout << ans << endl;
    }
    else{
        cout << -1 << endl;
    }
}
