#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define function function2
#define int long long int
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	int k=0;
	cin>>n;
	int temp=n;
	vector<int> vec;
	while(temp!=0)
	{
		vec.push_back(temp%10);
		temp/=10;
		k++;
	}
	int ones=0,twos=0,threes=0;
	for(int x:vec)
	{
		if(x%3==0)
		   threes++;
		else if(x%3==1)
		  ones++;
		else
		  twos++;
	}
	int val=threes;
	int i,j;
	for(i=0;i<=ones;i++)
	{
		for(j=0;j<=twos;j++)
		{
			int total=i+2*j;
			total+=3*threes;
			if(total%3==0)
			{
				val=max(val,i+j+threes);
			}
		}
	}
	if(val==0)
	  cout<<-1<<endl;
	else
	  cout<<k-val<<endl;
}

#include<bits/stdc++.h>

#define endl '\n'

using ll = long long;
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	string s;
	cin >> s;

	int n = s.length();

	int sum = 0;
	int ones = 0, twos = 0;
	for(char c : s) {
		int d = c-'0';
		sum += d;
		if(d % 3 == 1) ones++;
		if(d%3 == 2) twos++;
	}

	if(sum % 3 == 0) {
		cout << "0" << endl;
		return 0;
	}

	sum %= 3;
	if(sum == 1) {
		if(ones > 0 && n > 1) cout << 1;
		else if(twos > 1 && n > 2) cout << 2;
		else cout << "-1";
	}else if(sum == 2){
		if(twos > 0 && n > 1 ) cout << 1;
		else if(ones > 1 && n > 2) cout << 2;
		else cout << "-1";
	}

	return 0;
}
