#include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << endl
#define debug_4(x1, x2, x3, x4) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define debug(...)                                                        \
    CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
    (__VA_ARGS__)
#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
struct io
{
    io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

signed main(void)
{
    ll sum = 0, l = 0;
    string n;
    cin >> n;
    map<ll, ll> mp;
    l = n.length();
    rep(i, l)
    {
        sum += n[i] - '0';
        mp[(n[i] - '0') % 3]++;
    }
    if(sum%3==0)
    {
        mes(0);
        re0;
    }else if(sum%3==1)
    {
        if(mp[1]>0)
        {
            if (l == 1)
            {
                mes(-1);
                re0;
             }
            mes(1);
            re0;
        }else
        {
            if (l <=2)
            {
                mes(-1);
                re0;
            }
            mes(2);
            re0;
        }
        
    }else if(sum%3==2)
    {
        if (mp[2] > 0)
        {
            if (l == 1)
            {
                mes(-1);
                re0;
            }
            mes(1);
            re0;
        }
        else
        {
            if (l <= 2)
            {
                mes(-1);
                re0;
            }
            mes(2);
            re0;
        }
    }
    
}

#include <algorithm>
#include <array>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

int main() {
    long long int N, dig[20], k,  flag0 = 0, flag1 = 0, flag2 = 0, flag1_2 = 0,
                         flag2_2 = 0,ans=0,i,temp;
    cin >> N;
    temp=N;
    for(i = 1; i <= 20; i++) {
        dig[i] = N % 10;
        /*cout << dig[i]<< endl;*/
        N = (N - dig[i]) / 10;
        if(N < 1) {
            k = i;
            /*cout << k << endl;*/
            break;
        }
    }
    for(i = 1; i <= k; i++) {
        if(dig[i] % 3 == 1) {
            flag1 = 1;
        }
        if(flag1 == 1 && dig[i] % 3 == 1) {
            flag1_2 = 1;
        }
        if(dig[i] % 3 == 2) {
            flag2 = 1;
        }
        if(flag2 == 1 && dig[i] % 3 == 2) {
            flag2_2 = 1;
        }
    }

    N=temp;
    if(N % 3 == 0) {
        ans = 0;

    } 
    if(N % 3 == 1 && flag1 == 1 && k>1) {
        ans = 1;
    }
    if(N % 3 == 1 && flag1 == 1 && k<=1) {
        ans = -1;
    }

    if(N % 3 == 1 && flag1 != 1 && flag2_2 == 1 && k>2) {
        ans = 2;
    }
    if(N % 3 == 1 && flag1 != 1 && flag2_2 == 1 && k<=2) {
        ans = -1;
    }

    if(N % 3 == 1 && flag1 != 1 && flag2_2 != 1 && k>2) {
        ans = -1;
    }
    if((N % 3 == 2) && (flag2 == 1) && k>1) {
        ans = 1;
    }
    if((N % 3 == 2) && (flag2 == 1) && k<=1) {
        ans = -1;
    }
    if(N % 3 == 2 && flag2 != 1 && flag1_2 == 1 && k>2) {
        ans = 2;
    }
    if(N % 3 == 2 && flag2 != 1 && flag1_2 == 1 && k<=2) {
        ans = -1;
    }
    if(N % 3 == 2 && flag2 != 1 && flag1_2 != 1) {
        ans = -1;
    }
    cout << ans << endl;
}
