#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;

int main() {
  ll n;
  cin >> n;

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

  vector<int> a;
  do {
    a.push_back(n%10);
    n /= 10;
  } while (n!=0);

  if(a.size() == 1){
    cout << -1 << endl;
    return 0;
  }

  for(auto x : a){
    if( (accumulate(a.begin(), a.end(),0) - x)%3==0 ){
      cout << 1 << endl;
      return 0;
    }
  }

  if(a.size() < 3){
    cout << -1 << endl;
    return 0;
  }

  cout << 2 << endl;
  return 0;
}
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
int read()
{
	int x=0; bool flg=false; char ch=G;
	for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
	for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
	return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
	int res=1;
	for (;y;y>>=1,x=1LL*x*x%mod)
		if (y&1) res=1LL*res*x%mod;
	return res;
}
*/
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l);i<=int(r);i++)
#define per(i,l,r) for (int i(r);i>=int(l);i--)
#define all(x) (x).begin(),(x).end()

int n,w[64];
ll check(int c){
	ll z=c;
	rep(i,1,n){
		if (__gcd(c,w[i])>1) continue;
		int t=w[i]/__gcd(0LL+w[i],32LL*27*25*49);
		if (t==1) return ll(1e18);
		z*=t;
	}
	return z;
}
void solve(){
	n=read();
	rep(i,1,n) w[i]=read();
	ll z=1e18;
	int prm[8]={2,3,5,7};
	rep(i,0,15){
		int c=1;
		rep(j,0,3) if (i>>j&1) c*=prm[j];
		z=min(z,check(c));
	}
	printf("%lld\n",z);
}
int main()
{
	for (int T=1;T--;) solve();
	return 0;
}