#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define endl '\n'
#define mod 1000000007
//#define mod 998244353
#define inf 1e18
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define clr(x,n) x.clear();x.resize(n,0)
#define precise(x) fixed<<setprecision(x)
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> pbds;

/**
 *  author: Ashutosh Nayak (@nayakashutosh9)
 *	created: [2020-11-08 17:28]
 *	"Compete against yourself"
**/

const int N = 3e5 + 5;
string s; int ans, len;
void check(int i, int val, int cur) {
	if (i == len) {
		if (val % 3 == 0) ans = min(ans, len - cur);
		return;
	}
	check(i + 1, val, cur);
	int x = (int)(s[i] - '0');
	val = val * 10 + x; cur++;
	check(i + 1, val, cur);
}
void solve() {
	int n; cin >> n;
	s = "";
	int p = n;
	while (p) {
		s = (char)((p % 10) + '0') + s;
		p /= 10;
	}
	ans = inf; len = (int)s.length();
	check(0, 0, 0);
	if (ans == inf || ans == len) cout << "-1\n";
	else cout << ans << endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T; cin.ignore();
	for (int i = 1; i <= T; i++) {
		// cout << "Case #" << i << ": ";
		// clock_t start = clock();
		solve();
		// clock_t end = clock();
		// cout << (end-start) << endl;
	}

	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int n;
const int N = 25;
typedef long long ll;
ll ans = 1e18;
ll a[N];
void dfs(int p,ll s){
    if(p == n){
        ans = min(ans,s);
        return;
    }
    ll sum = 0;
    for(int i=p;i<n;i++){
        sum |= a[i];
        dfs(i+1,s^sum);
    }
}
int main(){
    cin >> n;
    for(int i=0;i<n;i++) cin >> a[i];
    dfs(0,0);
    cout << ans << endl;
    return 0;
}