#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,ans=0;
	bool flag=1;
	char s[100005];
	char head,tail;
	cin>>n>>s;
	if(n==2)
	{
		if(s[0]==s[1])
			ans=-1;
		else
			ans=1;
	}
	else if(n==3)
	{
		if(s[0]!=s[2])
			ans=1;
		else
			ans=-1;
	}
	else//n>3
	{
		if(s[0]!=s[n-1])
			ans=1;
		else
		{
			for(int i=2;i<=n-2;i++)
			{
				if(s[n-1-i]!=s[0] && s[n-i]!=s[0])
				{
					ans=2;
					flag=0;
					break;
				}
			}
			if(flag)
				ans=-1;
		}
	}
	cout<<ans<<endl;
	return 0;
}
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)

int N;
int A[200011];
int B[200011];

int U[400011];
int V[400011];
int E[400011];

int root(int x) {
    return x == U[x]? x: U[x] = root(U[x]);
}

void MAIN() {
    scanf("%d", &N);
    REP (i, 400011) {
	U[i] = i;
	V[i] = 1;
    }

    REP (i, N) {
	int a, b;
	scanf("%d%d", &a, &b);
	a = root(a);
	b = root(b);
	if (a != b) {
	    U[b] = a;
	    V[a] += V[b];
	    E[a] += E[b];
	}
	E[a]++;
    }

    int ans = 0;
    REP (i, 400011) if (U[i] == i) {
	ans += min(V[i], E[i]);
    }
    printf("%d\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

