#include <iostream>

using namespace std;
unsigned long long n,cn;
int s,cnt,nrcif,cf[19],i,r,j;
int main()
{
    cin>>n;
    cn=n;
    while(cn)
    {
        cf[++cnt]=(cn%10)%3;
        s+=cf[cnt];
        cn/=10;
        nrcif++;
    }
    r=s%3;
    if(r==0)
    {
        cout<<0;
        return 0;
    }
    int cnt1=1;
    if(cnt1<nrcif)
    {
        for(i=1; i<=cnt; i++)
        {
            if(r==cf[i])
            {
                cout<<cnt1;
                return 0;
            }
        }
        cnt1++;
        if(cnt1<nrcif)
        {
            for(i=1; i<=cnt; i++)
                for(j=i+1; j<=cnt; j++)
                {
                    if(r==cf[i]+cf[j])
                    {
                        cout<<cnt1;
                        return 0;
                    }
                }
            for(i=1; i<=n; i++)
            {
                if(cf[i]%3==0)
                {
                    cout<<nrcif-1;
                    return 0;
                }
            }
        }
    }
    cout<<-1;
    return 0;
}

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

int main() {
  ll n;
  cin >> n;
  vector<int> num(3);

  while (n) {
    num[n % 10 % 3]++;
    n /= 10;
  }
  int x = (num[1] + num[2] * 2) % 3;
  int k = num[0] + num[1] + num[2];
  if(x == 0){
    cout << 0 << endl;
  } else if (x == 1) {
    if (num[1]) {
      if (k == 1) {
        cout << -1 << endl;
      } else {
        cout << 1 << endl;
      }
    } else {
      if (k == 2) {
        cout << -1 << endl;
      } else {
        cout << 2 << endl;
      }
    }
  } else {
    if (num[2]) {
      if (k == 1) {
        cout << -1 << endl;
      } else {
        cout << 1 << endl;
      }
    } else {
      if (k == 2) {
        cout << -1 << endl;
      } else {
        cout << 2 << endl;
      }
    }
  }

  return 0;
}