#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;

int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int a, b, i, x, y;
    cin >> a >> b;
    for (i = b - a; i > 0; i--) {
        x = a / i;
        if (a % i)
            x++;
        y = b / i;
        if (x == y)
            continue;
        if (gcd(i * x, i * y) == i) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}
#include <bits/stdc++.h>
#define DB double
#define LL long long

#define MST(a,b) memset((a),(b),sizeof(a))

#ifdef _DEBUG_
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define MRK() ;
#define WRT(x) ;
#endif

#define MAXN 1010000
#define MAXM 2010000
#define MOD 998244353  //1000000007
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5

#define _ 0
using namespace std;

string s;
int n;
void init()
{

}

void work()
{
	cin>>n;
	cin>>s;
	if (s.front()!=s.back())
	{
		cout<<1<<endl;
		return ; 
	}
	for (int i=1;i<n-2;i++)
		if (s[i]!=s.back() && s[i+1]!=s.back())
		{
			cout<<2<<endl;
			return ;
		}
	cout<<-1<<endl;
		
}

int main()
{
	#ifdef _DEBUG_
	freopen("C:/Users/DELL/Documents/Dev-C++/sample.in","r",stdin);
	#endif
	init();
	int casenum=1;
	//scanf("%d",&casenum);
	for (int testcase=1;testcase<=casenum;testcase++)
	{
		#ifdef _DEBUG_
		printf("Case #%d:\n",testcase);
		#endif
		work();
	}
	return ~~(0^_^0);
}




