#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
using vl = vector<long long>;

int main(){
    string s; cin >> s;
    bitset<18> bb;
    bool flag = false;
    int ans = 0,cntmin=20;
    for(int ix=0;ix<(1<<(int)s.size());ix++){
        bb = ix;
        int tmp=0,cnt=0;
        for(int jx=0;jx<s.size();jx++){
            if(bb.test(jx)) tmp += (s.at(jx)-'0');
            else cnt++;
        }
        if(tmp%3 == 0 && tmp > 0 && cnt < cntmin){
            flag = true; 
            ans = cnt;
            cntmin = cnt;
        }
    }
    if(flag == true) cout << ans << endl;
    else cout << "-1" << endl;
}
#include <bits/stdc++.h>
using namespace std;

using ll_t = long long;

using digits_t = vector<int>;

digits_t get_digits(int n) {
  digits_t digits;

  while(n) {
    auto d = n%10;
    digits.push_back(d);
    n /= 10;
  }
  reverse(digits.begin(), digits.end());
  return digits;
}

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

  if(n%3 == 0) {
    cout << 0 << "\n";
  } else {
    digits_t digits = get_digits(n);
    for(int i = 1; i <= digits.size(); ++i) {
      vector<int> num(digits.size(), 0);
      for(int j = 1; j <= i; ++j) {
        num[j-1] = 1;
      }

      sort(num.begin(), num.end());
      do {
        int tmp = n;
        int sum = 0;
        for(int k = num.size(), p = 1; k >= 1; --k, ++p) {
          if(num[k-1] == 1) {
            sum += digits[k-1]*pow(10, p-1);
            //cout << "digit: " << digits[k-1] << "\n";
          }
        }
        //cout << "n: " << tmp << " sum: " << sum << "\n";

        if(tmp-sum == 0) {
          cout << -1 << "\n";
          return 0;
        }
        if((tmp-sum)%3 == 0) {
          cout << i << "\n";
          return 0;
        }
      } while(next_permutation(num.begin(), num.end()));
    }

    cout << -1 << "\n";
  }
  return 0;
}
