#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL Num;
LL Cnt[3],Loc;

int main() {
    cin>>Num;
    LL Temp=Num%3;
    while(Num) {
        Loc++;
        Cnt[Num%10%3]++;
        Num/=10;
    }
    if(Temp==0) printf("0\n"); 
    else if(Temp==1) {
		if(Cnt[1] >= 1 && 1 < Loc) printf("1\n"); 
		else if(Cnt[2]>=2 && 2<Loc) printf("2\n"); 
		else printf("-1\n"); 
	}
    else 
	{	
		if(Cnt[2]>=1 && 1<Loc) printf("1\n"); 
		else if(Cnt[1]>=2 && 2<Loc) printf("2\n");
		else printf("-1\n"); 
	}
    return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <cmath>
#include <set>
#include <map>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int inf = 10111111;

int main() {
	string s; cin >> s;
	vector<int>c(3);
	int n = s.size();
	rep(i, n)c[(s[i] - '0') % 3]++;
	int x = 0;
	rep(i, 3)x += c[i] * i;
	int ans = inf;
	rep(i1, 3)rep(i2, 3) {
		if (c[1] < i1)continue;
		if (c[2] < i2)continue;
		if (i1 + i2 == n)continue;
		int cal = x;
		cal -= 1 * i1;
		cal -= 2 * i2;
		if (cal % 3 == 0) {
			ans = min(ans, i1 + i2);
		}
	}
	if (ans == inf)ans = -1;
	cout << ans << endl;
}