#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;

int main(void){
	scanf("%d",&n);
	printf("%d\n",n-1);
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
//#pragma GCC optimize("O3")
using namespace std;
using namespace __gnu_pbds;
auto random_address = [] { char *p = new char; delete p; return uint64_t(p); };
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n,nr;
ll f[1001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    ll rest=n%3;
    if(rest==0)
    {
        cout<<0;
        return 0;
    }
    while(n)
    {
        ll cif=n%10;
        f[cif%3]++;
        n/=10;
        nr++;
    }
    if(rest==1)
    {
        if(f[1]>=1&&nr>1)
        {
            cout<<1;
            return 0;
        }
        else if(f[2]>=2&&nr>2)
        {
            cout<<2;
            return 0;
        }
        else
        {
            cout<<-1;
            return 0;
        }
    }
    if(rest==2)
    {
        if(f[2]>=1&&nr>1)
        {
            cout<<1;
            return 0;
        }
        else if(f[1]>=2&&nr>2)
        {
            cout<<2;
            return 0;
        }
        else
        {
            cout<<-1;
            return 0;
        }
    }
    return 0;
}
