#include <bits/stdc++.h>

using namespace std;

void fastStd()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	fastStd();

	string s;
	cin >> s;
	int n = s.length();
	vector<int> arr(n);

	for(int i = 0; i < n; i++){
		arr[i] = (s[i] - '0') % 3;
	}

	vector<vector<int>> dp(n, vector<int>(3, 0));
	// dp[0][arr[0]] = 1;
	for(int i = 0; i < n; i++){
		dp[i][arr[i]] = 1;
	}
	for(int i = 1; i < n; i++){
		for(int j = 0; j < 3; j++){
			// int a = dp[i][(j+arr[i])%3] + dp[i-1][j];
			if(dp[i-1][j] != 0)
				dp[i][(j+arr[i])%3] = max(dp[i-1][j] + 1, dp[i-1][(j+arr[i])%3]);
			else
				dp[i][(j+arr[i])%3] = max(dp[i-1][(j+arr[i])%3], dp[i][(j+arr[i])%3]);
		}
		// for(int j = 0; j < 3; j++){
		// 	printf("%d ", dp[i][j]);
		// }
		// printf("\n");
	}
	if(dp[n-1][0] != 0){
		cout << n - dp[n-1][0] << "\n";
	} else {
		cout << -1 << "\n";
	}


	
	return 0;
}
#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007
#define deb(x) cout << #x << " = " << x << endl
#define deb2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626

typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    
    string s;
    cin>>s;
    int n = s.length();
    int ar[n];
    

    int sum = 0;

    for(int i=0; i<n; i++)ar[i] = ((s[i]-'0')%3);
    for(int i=0; i<n; i++)sum+=ar[i];

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

    int yo = sum%3;
    int c[3];
    c[0] = 0; c[1] = 0; c[2] = 0;

    for(int i=0; i<n; i++)
    {
        c[ar[i]]++;
    }

    if(yo==1)
    {
        if(c[1]>0)
        {
            if(n>1)cout<<"1";
            else cout<<"-1";
        }
        else if(c[2]>1)
        {
            if(n>2)cout<<"2";
            else cout<<"-1";
        }
        else cout<<"-1";
    }
    else
    {
        if(c[2]>0)
        {
            if(n>1)cout<<"1";
            else cout<<"-1";
        }
        else if(c[1]>1)
        {
            if(n>2)cout<<"2";
            else cout<<"-1";
        }
        else cout<<"-1";
    }
    

    return 0;
}