#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vi;
typedef vector<ll> vll;
#define all(v) v.begin(), v.end()
#define rep(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define lep(i,s,n) for(ll i=(ll)(s);i<(ll)(n);i++)
#define vep(v) for(auto&& x : v)
#define sz(s) (int)(s.size())
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using graph = vector<vector<int>>;

int main(){
  string s;
  cin >> s;
  int n = sz(s), cnt = 0, ans;
  vi p(3);
  rep(i, 0, n){
    int q = s[i]-'0';
    q%=3;
    cnt += q;
    p[q]++;
  }
  cnt%=3;
  if(cnt==0){
    cout << 0 << endl;
    return 0;
  }
  if(cnt==2){
    if(p[2])ans = 1;
    else if(p[1]>=2)ans = 2;
    else ans = -1;
  }
  else if(cnt==1){
    if(p[1])ans = 1;
    else if(p[2]>=2)ans = 2;
    else ans = -1;
  }
  if(ans>=n)cout << -1 << endl;
  else cout << ans << endl;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
    string s;
    cin >> s;
    vector<int>a(10,0);
    int sum=0;
    for(int i=0; i<s.size(); i++){
        sum += s[i]-'0';
        a[s[i]-'0']++;
    }
    if(sum%3==0){
        cout << 0 << endl;
        return 0;
    }else if(sum%3==1){
        if(s.size()==1){
            puts("-1");
        }else if(a[1]||a[4]||a[7]){
            cout << 1 << endl;
        }else if(s.size()==2){
            puts("-1");
        }else if(a[2]>=1&&a[5]>=1){
            cout << 2 << endl;
        }else if(a[2]>=1&&a[8]>=1){
            cout << 2 << endl;
        }else if(a[8]>=1&&a[5]>=1){
            cout << 2 << endl;
        }else if(a[2]>=2){
            cout << 2 << endl;
        }else if(a[5]>=2){
            cout << 2 << endl;
        }else if(a[8]>=2){
            cout << 2 << endl;
        }else{
            puts("-1");
        }
    }else{
        if(s.size()==1){
            puts("-1");
        }else if(a[2]||a[5]||a[8]){
            cout << 1 << endl;
        }else if(s.size()==2){
            puts("-1");
        }else if(a[1]>=1&&a[4]>=1){
            cout << 2 << endl;
        }else if(a[1]>=1&&a[7]>=1){
            cout << 2 << endl;
        }else if(a[4]>=1&&a[7]>=1){
            cout << 2 << endl;
        }else if(a[1]>=2){
            cout << 2 << endl;
        }else if(a[4]>=2){
            cout << 2 << endl;
        }else if(a[7]>=2){
            cout << 2 << endl;
        }else{
            puts("-1");
        }
    }
    return 0;
}