//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define re register int
#define ll long long
#define I inline int
#define V inline void
#define LL inline ll
#define FOR(i,a,b) for(re i=(a),i##i=(b);i<=i##i;++i)
#define ROF(i,a,b) for(re i=(a),i##i=(b);i>=i##i;--i)
//#define gc (_ft==_fs&&(_ft=(_fs=buf)+fread(buf,1,1<<20,stdin),_ft==_fs))?0:*_fs++
#define gc getchar()
using namespace std;
const int N=1e5+10,S=1200,mo=1e9+7;
char buf[1<<20],*_fs,*_ft;
LL read(){
	ll w=0,p=0; char ch=gc;
	while(!isdigit(ch)) w=ch=='-',ch=gc;
	while(isdigit(ch))  p=p*10+ch-'0',ch=gc;
	return w?-p:p;
}
char a[N];
int n,ans;
V ipt(){ n=read(); scanf("%s",a+1); }
V sol(){
	ans=-1;
	if(a[1]!=a[n]) return ans=1,void();
	FOR(i,2,n-1) if(a[i]!=a[1]&&a[i+1]!=a[n]) return ans=2,void();
	return ;
} 
V opt(){ cout<<ans; }
int main(){
	ipt();
	sol();
	opt();
	return 0;
}
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define repl(i,l,r) for(int i=l;i<r;i++)
#define rrepl(i,l,r) for(int i=r-1;i>=l;i--)
const int inf=1e9+10;
const ll INF=2e18+10;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

const ll MOD=1e9+7;

int main(){
    ll n;cin>>n;
    ll l,r;
    l=0,r=INF;
    ll ans;
    while(l+1<r){
        ll mid=(l+r)/2;
        if((n+1)*2/mid+1<mid-1){
            r=mid;
            continue;
        }
        ll sum=mid*(mid+1)/2;
        //cout<<mid<<" "<<sum<<endl;
        if(sum>n+1)r=mid;
        else l=mid;
    }
    ans=n-l+1;
    //if(ans==1)ans=1;
    //if(ans==2)ans=1;

    cout<<ans<<endl;
    
    return 0;
}