// CTRL+ALT+M 
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define int long long int
#define vi vector<int> 
#define vb vector<bool>
#define vc vector<char>
#define vpi vector<pair<int,int>>
#define pii pair<int,int>
#define pb push_back
#define inf 1e18
#define ff first
#define ss  second
#define deb(x) cout<<#x<<" "<<x<<endl;
#define all(x) x.begin(),x.end()
#define endl "\n"
const int maxn=1e5;
//*! look for binary search see if dp can fit or dp with bs 
//*? Set or Priority_queue
//*! Number is upto 10^5 check if a[maxn] gives something
//*? Graph Question depedencies Topo sort
//todo For a Tree its shortest Path in  dfs is same as in bfs if it is acyclic
// *! Bridge Detection:: From Backedge we take low[u]=min(low[u],tin[x]) 
// *! Bridge Detection:: From Child we take low[u]=min(low[u],low[x]) 
// *! Bridge Detection:: this is a bridge if(low[x]>tin[u]) 
// vi adj[maxn];
// int visited[maxn];
int dp[20][10];
int solve(int s,int n,string & p,int sum)
{
  if(s>=n)
  {
    if(sum==0)
    {
      return 0;
    }
    return INT_MAX;
  }
  if(dp[s][sum]!=-1)
  {
    return dp[s][sum];
  }
   int l=sum+(p[s]-'0');
   l=l%3;
   int op1=solve(s+1,n,p,l);
   int op2=1+solve(s+1,n,p,sum);
   return  dp[s][sum]=min(op1,op2);
}
void runcases(int T)
{

   string s;
   cin>>s;
   int n=s.size();
   memset(dp,-1,sizeof(dp));
   int f=solve(0,n,s,0);
    if(f==n)
    {
      cout<<-1<<endl;

    }else cout<<f<<endl;

  

  //cout<<"Case #"<<T<<": ";
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T = 1;
    //cin >> T;
    for (int t=1;t<=T; t++) {
        runcases(t);
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n = s.length(),sum=0;
        vector<int> c(3,0);
        if(n==1)
        {
            if(int(s[0]-'0')%3==0)cout<<"0";
            else cout<<"-1";
            
            continue;
        }
        for(int i=0;i<n;i++)
        {
            int a = int(s[i]-'0')%3;
            c[a]++;
            sum+=a;
        }
        sum = sum%3;
        if(sum==0)cout<<"0";
        else if(sum==2)
        {
            if(c[2]>0){cout<<"1";continue;}
            
            if(c[1]>1 && n>2){cout<<"2";continue;}
            cout<<"-1";
        }
        else
        {
            if(c[1]>0)cout<<"1";
            else if(n>2 && c[2]>1)cout<<"2";
            else
            cout<<"-1";
        }
        
    }
    
	return 0;
}
