//~ author      : Sumit Prajapati
#include <bits/stdc++.h>
using namespace std;


#define ull                 unsigned long long
#define ll                  long long
// #define int                 long long           
#define pii                 pair<int, int>
#define pll                 pair<ll, ll>
#define pb                  push_back
#define mk                  make_pair
#define ff                  first
#define ss                  second
#define all(a)              a.begin(),a.end()
#define trav(x,v)           for(auto &x:v)
#define rep(i,n)            for(int i=0;i<n;i++)
#define repe(i,n)           for(int i=1;i<=n;i++)
#define read(a,n)           rep(i,n)cin>>a[i]
#define reade(a,n)          repe(i,n)cin>>a[i]
#define FOR(i,a,b)          for(int i=a;i<=b;i++)
#define printar(a,s,e)      FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n'
#define curtime             chrono::high_resolution_clock::now()
#define timedif(start,end)  chrono::duration_cast<chrono::nanoseconds>(end - start).count()




auto time0 = curtime;
const int MD=1e9+7;
const int MDL=998244353;
const int INF=1e9;
const int MX=1e5+5;

 
 
 
 
void solve(){
  
    int n;
    string s;
    cin>>n;
    cin>>s;
    if(s[0]!=s[n-1]){
        cout<<"1\n";
        return;
    }
    FOR(i,0,n-2)
        if(s[i]!=s[0] && s[i+1]!=s[0]){
            cout<<"2\n";
            return;
        }
    cout<<"-1\n";
 
} 
 
 
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    time0 = curtime;
    srand(time(NULL));
    int t=1;
    // cin>>t;
    repe(tt,t){
        // cout<<"Case #"<<tt<<": ";
        solve();
    }
    
    // cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n";
    return 0;
 
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define mp make_pair
#define lli long long int
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define mii map<lli,lli>
#define msi map<string,lli>
#define pii pair<lli,lli> 
#define vi vector<lli> 
#define vii vector<pair<lli,lli>> 
#define vvi vector< vector<lli> >
#define sz(x) (lli)x.size()
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX1 998244353
#define MAX2 1000000007

// Policy based data structure 
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> Ordered_set;
// Ordered_set s;
// *s.find_by_order(a);
// s.order_of_key(a);
// s.erase(s.find_by_order( s.order_of_key(v[i-k]) ));

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#define input(data,n,command) for(lli i=0;i<n;i++){lli tmp;cin>>tmp;data.pb(tmp);command}
#define sort_asc(a) sort(a.begin(),a.end());
//#define sort_desc(a) sort(a.begin(),a.end(),greater<lli>());
#define display(a) for(lli i = 0 ;i<a.size();i++){cout<<a[i]<<" ";} cout<<endl;
#define rep(i,a,b) for(lli i = a;i < b;i++)
#define endl "\n"
#define INF 1000000000000000000
#define MAXN 100010

//Written By MUDIT BHARDWAJ...
/*--------------------------------------------------------------------*/
//vector<pair<lli,lli>>adj[300010];
lli n;
vector<pair<lli,pii>> v;

lli intersect(pair<lli,pii> tmp1,pair<lli,pii> tmp2)
{
    lli t1,l1,r1,t2,l2,r2;
    t1 = tmp1.F;
    l1 = tmp1.S.F;
    r1 = tmp1.S.S;
    t2 = tmp2.F;
    l2 = tmp2.S.F;
    r2 = tmp2.S.S;
    lli l = max(l1,l2);
    lli r = min(r1,r2);
    if(l < r)
    {
        return true;
    }
    else if(l == r)
    {
        if(l1 <= l2)
        {
            if(((t1 != 2)&&(t1 != 4)&&(t2 != 3)&&(t2 != 4)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            if(((t1 != 3)&&(t1 != 4)&&(t2 != 2)&&(t2 != 4)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
}

lli solve()
{
    //auto start = chrono::steady_clock::now();

    //start from here
    cin>>n;
    rep(i,0,n)
    {
        lli a,b,c;
        cin>>a>>b>>c;
        v.pb(mp(a,mp(b,c)));
    }

    lli ans = 0;
    for(lli i = 0;i<n;i++)
    {
        for(lli j = i+1;j<n;j++)
        {
            if(intersect(v[i],v[j]))
            {
                ans++;
            }
        }
    }
    cout<<ans<<endl;

    //auto end = chrono::steady_clock::now();
    //cout << "Elapsed time in milliseconds : "<< chrono::duration_cast<chrono::milliseconds>(end - start).count()<< " ms" << endl;
    return 0;
}

int main()
{
    ios
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    lli T=1;
    //cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}
/*-----------------------------------------------------------------------*/