//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <iostream>
//#include <unordered_set>
#include <unordered_map>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define int ll
typedef long long ll;
typedef long double ld;
//#pragma comment(linker, "/STACK:66777216")
using namespace std;

const ll INF = 1e18, MOD = 998244353, MAXN = 1048576, MAXM = 100;

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

signed main() {
    //ifstream  cin("counting.in");
    //ofstream cout("counting.out");
    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    string s;
    cin >> s;
    int cur = 1, cnt = 1, ok = 0, len = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == s[i - 1]) cur++;
        else {
            cnt++;
            len = cur;
            if (cur > 1) ok++;
            cur = 1;
        }
    }
    if (cur > 1) ok++, len = cur;
    if (ok == 1 && len == 2) cout << cnt + 1;
    else if (ok)
        cout << cnt + 2;
    else
        cout << cnt;
    //cout << cnt + min(2ll, ok);
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <iostream>
//#include <unordered_set>
#include <unordered_map>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define int ll
typedef long long ll;
typedef long double ld;
//#pragma comment(linker, "/STACK:66777216")
using namespace std;

const ll INF = 1e18, MOD = 998244353, MAXN = 1048576, MAXM = 100;

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

signed main() {
    //ifstream  cin("counting.in");
    //ofstream cout("counting.out");
    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    string s;
    cin >> s;
    int cur = 1, cnt = 1, ok = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == s[i - 1]) cur++;
        else {
            cnt++;
            if (cur > 2) ok = 1;
            cur = 1;
        }
    }
    cout << cnt + ok * 2;
}


#include <stdio.h>
#include <vector>
#include <functional>
#include <numeric>
#include <complex>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <assert.h>
#include <sstream>
#include <string>

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define X first
#define Y second
#define pb(x) push_back(x)
#define FOR(k, n) for (int i = k; i < n; i++)
#define debug(x) printf("x = %d\n", x);
#define debug2(x, y) printf("x = %d, y = %d\n", x, y);
#define debug3(x, y, z) printf("x = %d, y = %d, z = %d\n", x, y, z);
#define debug4(x, y, z, t) printf("x = %d, y = %d, z = %d, t = %d\n", x, y, z, t);

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef complex<double> cd;

const int MOD = 1000000007;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const double PI = acos(-1);

FILE * in = stdin;
FILE * out = stdout;

// Solution

const int MAXN = 100005;

int n;
char str[MAXN];

vector<int> compress() {
    vector<int> res = {str[0]};

    for (int i = 1; i < n; i++) {
        if (str[i] != str[i - 1]) {
            res.push_back(str[i]);
        }
    }

    return res;
}

int main() {
    scanf("%d", &n);
    scanf(" %s", str);

    vector<int> res = compress();

    printf("%d\n", min(n, sz(res) + 2));
    return 0;
}


#include <stdio.h>
#include <vector>
#include <functional>
#include <numeric>
#include <complex>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <assert.h>
#include <sstream>
#include <string>

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define X first
#define Y second
#define pb(x) push_back(x)
#define FOR(k, n) for (int i = k; i < n; i++)
#define debug(x) printf("x = %d\n", x);
#define debug2(x, y) printf("x = %d, y = %d\n", x, y);
#define debug3(x, y, z) printf("x = %d, y = %d, z = %d\n", x, y, z);
#define debug4(x, y, z, t) printf("x = %d, y = %d, z = %d, t = %d\n", x, y, z, t);

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef complex<double> cd;

const int MOD = 1000000007;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const double PI = acos(-1);

FILE * in = stdin;
FILE * out = stdout;

// Solution

const int MAXN = 1000005;

int n, DP[MAXN][2];
char str[MAXN];
bool flag;

void eval() {
    DP[0][0] = str[0] == '0';
    DP[0][1] = str[0] == '1';

    for (int i = 1; i < n; i++) {
        if (str[i] == '0') {
            DP[i][0] = DP[i - 1][1] + 1;
            DP[i][1] = DP[i - 1][1];
        } else {
            DP[i][0] = DP[i - 1][0];
            DP[i][1] = DP[i - 1][0] + 1;
        }
    }
}

int main() {
    scanf("%d", &n);
    scanf(" %s", str);

    for (int i = 2;  i < n; i++) {
        if (str[i] == str[i - 1] && str[i - 1] == str[i - 2]) {
            flag = true;
        }
    }
    eval();

    printf("%d\n", max(DP[n - 1][0], DP[n - 1][1]) + 2 * flag);

    return 0;
}


#include<iostream>
#include<unordered_map>
#include<bitset>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>
#include<map>
#include<stack>
#include<iterator>
#include<cstring>

#include<deque>
#define pi 3.1415926535
#include<chrono>

#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f0 get<0>
#define f1 get<1>
#define f2 get<2>

using namespace std;
ll n;
ll ncr(ll n, ll r)
{
    ll i, ans = 1, k = 0;
    if (r == 0)
        return 1;
    else if (n < r)
        return 0;
    for (i = n; i >= n - r + 1; i--)
    {
        ans *= i;
        ans /= n - i + 1;
    }

    return ans;

}

void max_heapify(int a[100], int i)
{
    int l, r, largest = i;
    l = 2 * i + 1;
    r = 2 * i + 2;
    if (l<n && a[l]>a[i])
        largest = l;
    if (r<n && a[r]>a[l])
        largest = r;
    if (largest != i)
    {
        int k;
        k = a[i];
        a[i] = a[largest];
        a[largest] = k;
        max_heapify(a, largest);
    }

}
int power(ll x, ll y, ll p)
{
    int res = 1;
    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

bool square(ll n)
{
    double x;
    x = sqrt(n);
    if (x == floor(x))
        return true;
    else return false;
}

bool prime(ll n)
{
    ll i;
    for (i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

ll modi(ll a, ll m)
{
    return power(a, m - 2, m);

}
void kmp(string s, ll p[]) {
    int n = (int)s.length();
    p[0] = 0;
    for (int i = 1; i < n; i++) {
        int j = p[i - 1];
        while (j > 0 && s[i] != s[j])
            j = p[j - 1];
        if (s[i] == s[j])
            j++;
        p[i] = j;
    }

}
void zfunc(string s, ll z[]) {

    ll n = (ll)s.length();
    z[0] = 0;
    for (ll i = 1, l = 0, r = 0; i < n; ++i) {
        if (i <= r)
            z[i] = min(r - i + 1, z[i - l]);
        while (i + z[i] < n && s[z[i]] == s[i + z[i]])
            ++z[i];
        if (i + z[i] - 1 > r)
            l = i, r = i + z[i] - 1;
    }

}
struct pair_hash {
    size_t operator()(const pair<int, int>& p) const noexcept {
        return hash<ll>() ((((ll)p.first) << 32) | ((ll)p.second));
    }
};

bool palin(string s) {
    ll i, n;
    n = s.length();
    for (i = 0; i <= n / 2; i++) {
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}
ll sum(ll n) {
    ll i;
    i = n;
    ll sum = 0;
    while (i > 0) {
        sum += i % 10;
        i = i / 10;
    }
    return sum;
}

ll nCr(ll n, ll r, ll p, ll fac[])
{

    if (r == 0)
        return 1;
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = fac[i - 1] * i % p;

    return (fac[n] * modi(fac[r], p) % p *
        modi(fac[n - r], p) % p) % p;
}

ll gcd(ll a, ll b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll mod(string num, ll a)
{

    ll res = 0;

    for (ll i = 0; i < num.length(); i++)
        res = (res * 10 + (ll)num[i] - '0') % a;

    return res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll parent[1000], sizer[1000];
ll findset(ll a) {
    if (a == parent[a]) {
        return a;
    }
    parent[a] = findset(parent[a]);
    return parent[a];
}

void unionset(ll a, ll b) {
    a = findset(a);
    b = findset(b);
    if (sizer[a] < sizer[b]) {
        swap(a, b);
    }
    sizer[b] += sizer[a];
    parent[b] = a;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;

    //cin >> T;
    while (T--) {
        ll n;
        cin >> n;
        string s;
        cin >> s;
        ll dp[100][2] = { 0 };
        for (i = 0; i < n; i++) {
            if (s[i] == '0') {
                dp[i + 1][0] = max(dp[i][0], dp[i][1] + 1);
                dp[i + 1][1] = dp[i][1];
            }
            else {
                dp[i + 1][1] = max(dp[i][1], dp[i][0] + 1);
                dp[i + 1][0] = dp[i][0];
            }
        }
        cout << min(2 + max(dp[n][1], dp[n][0]), n);
    }

    return 0;
}


#include<iostream>
#include<unordered_map>
#include<bitset>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>
#include<map>
#include<stack>
#include<iterator>
#include<cstring>

#include<deque>
#define pi 3.1415926535
#include<chrono>

#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f0 get<0>
#define f1 get<1>
#define f2 get<2>

using namespace std;
ll n;
ll ncr(ll n, ll r)
{
    ll i, ans = 1, k = 0;
    if (r == 0)
        return 1;
    else if (n < r)
        return 0;
    for (i = n; i >= n - r + 1; i--)
    {
        ans *= i;
        ans /= n - i + 1;
    }

    return ans;

}

void max_heapify(int a[100], int i)
{
    int l, r, largest = i;
    l = 2 * i + 1;
    r = 2 * i + 2;
    if (l<n && a[l]>a[i])
        largest = l;
    if (r<n && a[r]>a[l])
        largest = r;
    if (largest != i)
    {
        int k;
        k = a[i];
        a[i] = a[largest];
        a[largest] = k;
        max_heapify(a, largest);
    }

}
int power(ll x, ll y, ll p)
{
    int res = 1;
    x = x % p;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

bool square(ll n)
{
    double x;
    x = sqrt(n);
    if (x == floor(x))
        return true;
    else return false;
}

bool prime(ll n)
{
    ll i;
    for (i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

ll modi(ll a, ll m)
{
    return power(a, m - 2, m);

}
void kmp(string s, ll p[]) {
    int n = (int)s.length();
    p[0] = 0;
    for (int i = 1; i < n; i++) {
        int j = p[i - 1];
        while (j > 0 && s[i] != s[j])
            j = p[j - 1];
        if (s[i] == s[j])
            j++;
        p[i] = j;
    }

}
void zfunc(string s, ll z[]) {

    ll n = (ll)s.length();
    z[0] = 0;
    for (ll i = 1, l = 0, r = 0; i < n; ++i) {
        if (i <= r)
            z[i] = min(r - i + 1, z[i - l]);
        while (i + z[i] < n && s[z[i]] == s[i + z[i]])
            ++z[i];
        if (i + z[i] - 1 > r)
            l = i, r = i + z[i] - 1;
    }

}
struct pair_hash {
    size_t operator()(const pair<int, int>& p) const noexcept {
        return hash<ll>() ((((ll)p.first) << 32) | ((ll)p.second));
    }
};

bool palin(string s) {
    ll i, n;
    n = s.length();
    for (i = 0; i <= n / 2; i++) {
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}
ll sum(ll n) {
    ll i;
    i = n;
    ll sum = 0;
    while (i > 0) {
        sum += i % 10;
        i = i / 10;
    }
    return sum;
}

ll nCr(ll n, ll r, ll p, ll fac[])
{

    if (r == 0)
        return 1;
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = fac[i - 1] * i % p;

    return (fac[n] * modi(fac[r], p) % p *
        modi(fac[n - r], p) % p) % p;
}

ll gcd(ll a, ll b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll mod(string num, ll a)
{

    ll res = 0;

    for (ll i = 0; i < num.length(); i++)
        res = (res * 10 + (ll)num[i] - '0') % a;

    return res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll parent[1000], sizer[1000];
ll findset(ll a) {
    if (a == parent[a]) {
        return a;
    }
    parent[a] = findset(parent[a]);
    return parent[a];
}

void unionset(ll a, ll b) {
    a = findset(a);
    b = findset(b);
    if (sizer[a] < sizer[b]) {
        swap(a, b);
    }
    sizer[b] += sizer[a];
    parent[b] = a;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;

    //cin >> T;
    while (T--) {
        ll n;
        cin >> n;
        string s;
        cin >> s;
        ll dp[100001][2] = { 0 };
        for (i = 0; i < n; i++) {
            if (s[i] == '0') {
                dp[i + 1][0] = max(dp[i][0], dp[i][1] + 1);
                dp[i + 1][1] = dp[i][1];
            }
            else {
                dp[i + 1][1] = max(dp[i][1], dp[i][0] + 1);
                dp[i + 1][0] = dp[i][0];
            }
        }
        cout << min(2 + max(dp[n][1], dp[n][0]), n);
    }

    return 0;
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
		a = (a*a) % mod;
	}
	return ans;
}
ll C(ll n, ll m)//��������ж�
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = ans*(n - m + i) / i;
	return ans;
}
bool is_prime(int x)//�����жϣ�����ɸ�����ĵ�
{
	for (int i = 2;i <= sqrt(1.0*x);i++)
		if (x%i == 0) return false;
	return true;
}*/
string s;
int n;
bool f2 = false, f3 = false;
int main()
{
	cin >> n >> s;
	int cnt2 = 0;
	for (int i = 0;i < n-1;i++)
	{
		if (s[i] == s[i + 1])
		{
			cnt2++;
			f2 = true;
			break;
		}
	}

	for (int i = 0;i < n - 2;i++)
	{
		if (s[i] == s[i + 1]&&s[i]==s[i+2])
		{
			f3 = true;
			break;
		}
	}
	int cnt = 1;
	char c = s[0];
	for (int i = 1;i < n;i++)
	{
		if (s[i] != c)
		{
			cnt++;
			c = s[i];
		}
	}
	if (f3||cnt2==2) cout << cnt + 2;
	else if (f2) cout << cnt + 1;
	else cout << cnt;
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
		a = (a*a) % mod;
	}
	return ans;
}
ll C(ll n, ll m)//��������ж�
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = ans*(n - m + i) / i;
	return ans;
}
bool is_prime(int x)//�����жϣ�����ɸ�����ĵ�
{
	for (int i = 2;i <= sqrt(1.0*x);i++)
		if (x%i == 0) return false;
	return true;
}*/
string s;
int n;
int main()
{
	cin >> n >> s;

	int cnt = 1;
	char c = s[0];
	for (int i = 1;i < n;i++)
	{
		if (s[i] != c)
		{
			cnt++;
			c = s[i];
		}
	}
	cout << min(n, cnt + 2);
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
		a = (a*a) % mod;
	}
	return ans;
}
ll C(ll n, ll m)//��������ж�
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = ans*(n - m + i) / i;
	return ans;
}
bool is_prime(int x)//�����жϣ�����ɸ�����ĵ�
{
	for (int i = 2;i <= sqrt(1.0*x);i++)
		if (x%i == 0) return false;
	return true;
}*/
string s;
int n;
bool f2 = false, f3 = false;
int main()
{
	cin >> n >> s;
	for (int i = 0;i < n-1;i++)
	{
		if (s[i] == s[i + 1])
		{
			f2 = true;
			break;
		}
	}
	for (int i = 0;i < n - 2;i++)
	{
		if (s[i] == s[i + 1]&&s[i]==s[i+2])
		{
			f3 = true;
			break;
		}
	}
	int cnt = 1;
	char c = s[0];
	for (int i = 1;i < n;i++)
	{
		if (s[i] != c)
		{
			cnt++;
			c = s[i];
		}
	}
	if (f3) cout << cnt + 2;
	else if (f2) cout << cnt + 1;
	else cout << cnt;
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
		a = (a*a) % mod;
	}
	return ans;
}
ll C(ll n, ll m)//��������ж�
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = ans*(n - m + i) / i;
	return ans;
}
bool is_prime(int x)//�����жϣ�����ɸ�����ĵ�
{
	for (int i = 2;i <= sqrt(1.0*x);i++)
		if (x%i == 0) return false;
	return true;
}*/
string s;
int n;
bool f2 = false, f3 = false;
int main()
{
	cin >> n >> s;
	int cnt2 = 0;
	for (int i = 0;i < n-1;i++)
	{
		if (s[i] == s[i + 1])
		{
			cnt2++;
			f2 = true;
		}
	}

	for (int i = 0;i < n - 2;i++)
	{
		if (s[i] == s[i + 1]&&s[i]==s[i+2])
		{
			f3 = true;
			break;
		}
	}
	int cnt = 1;
	char c = s[0];
	for (int i = 1;i < n;i++)
	{
		if (s[i] != c)
		{
			cnt++;
			c = s[i];
		}
	}
	if (f3||cnt2==2) cout << cnt + 2;
	else if (f2) cout << cnt + 1;
	else cout << cnt;
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
		a = (a*a) % mod;
	}
	return ans;
}
ll C(ll n, ll m)//��������ж�
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = ans*(n - m + i) / i;
	return ans;
}
bool is_prime(int x)//�����жϣ�����ɸ�����ĵ�
{
	for (int i = 2;i <= sqrt(1.0*x);i++)
		if (x%i == 0) return false;
	return true;
}*/
string s;
int n;
bool f2 = false, f3 = false;
int main()
{
	cin >> n >> s;
	int cnt2 = 0;
	for (int i = 0;i < n-1;i++)
	{
		if (s[i] == s[i + 1])
		{
			cnt2++;
			f2 = true;
		}
	}

	for (int i = 0;i < n - 2;i++)
	{
		if (s[i] == s[i + 1]&&s[i]==s[i+2])
		{
			f3 = true;
			break;
		}
	}
	int cnt = 1;
	char c = s[0];
	for (int i = 1;i < n;i++)
	{
		if (s[i] != c)
		{
			cnt++;
			c = s[i];
		}
	}
	if (f3||cnt2>=2) cout << cnt + 2;
	else if (f2) cout << cnt + 1;
	else cout << cnt;
	return ~~(0 - 0);
}


#include <iostream>
#include <string>
using namespace std;
int n;
string s;
bool usedflip = false;
int main() {
	cin >> n >> s;
	bool cur = !(s[0] - '0');
	int ans = 0;
	int pos = 0;
	for (int i = 0; i < s.length(); i++) {
		if (cur && s[i] == '0') {
			ans++;
			cur = false;
		}
		else if (!cur && s[i] == '1') {
			ans++;
			cur = true;
		}
		else pos++;
	}
	if (pos > 2)
		pos = 2;
	cout << ans + pos << '\n';
}


#include <iostream>
using namespace std;
int n;
string s;
int main() {
	cin >> n >> s;
	for (int i = 0; i < s.length() - 2; i++) {
		if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == '1') {
			s[i + 1] = '0';
			break;
		}
		if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == '0') {
			s[i + 1] = '1';
			break;
		}
	}
	bool cur = (s[0] - '0');
	int ans = 1;
	for (int i = 0; i < s.length(); i++) {
		if (cur && s[i] == '0') {
			ans++;
			cur = false;
		}
		else if (!cur && s[i] == '1') {
			ans++;
			cur = true;
		}
	}
	cout << ans << '\n';
}


#include <iostream>
#include <string>
using namespace std;
int n;
string s;
int main() {
	cin >> n >> s;
	for (int i = 0; i < s.length() - 2; i++) {
		if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == '1') {
			s[i + 1] = '0';
			break;
		}
		if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == '0') {
			s[i + 1] = '1';
			break;
		}
	}
	bool cur = (s[0] - '0');
	int ans = 1;
	for (int i = 0; i < s.length(); i++) {
		if (cur && s[i] == '0') {
			ans++;
			cur = false;
		}
		else if (!cur && s[i] == '1') {
			ans++;
			cur = true;
		}
	}
	cout << ans << '\n';
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>

using namespace std;

#define ll long long
#define sc second
#define fs first
#define mp make_pair
#define pb push_back

ll n, k, a[111111];

char swap(char c)
{
	if (c == '0') return '1';
	else return '0';
}

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	int sz = 1, em = 0;
	char c = s[0];
	for (int i = 1; i < s.size(); i++){
		if (s[i] != c)
			c = swap(c), sz++;
		else em++;
	}
	if (s.size() != sz)
		sz = sz + 1 + (em>1);
	cout << sz;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	int cnt = 0;
	int dcnt = 0;
	for (int i = 1; i < (int)s.length(); ++i)
	{
		if (s[i] != s[i - 1])
			++cnt;
		else
			++dcnt;
	}
	if (dcnt > 2)
		dcnt = 2;
	cnt += dcnt;
	cout << cnt + 1 << endl;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int M = 205;
const int inf = 1000000007;
const int mod = 2009;
char s[N];
int w[N];
int main()
{
    int n,i,k,p=0,Max=0,t=0;
    bool flag=false;
    scanf("%d",&n);
    scanf("%s",s);
    for(k=1,i=1;i<n;i++)
        if(s[i]!=s[i-1])
        {
            w[p++]=k;
            if(k>1)
                t++;
            Max=max(Max,k);
            k=1;
        }
        else
            k++;
    w[p++]=k;
    if(k>1)
        t++;
    Max=max(Max,k);
    if(Max>2||t>1)
        printf("%d\n",2+p);
    else if(Max>1)
        printf("%d\n",1+p);
    else
        printf("%d\n",p);
    return 0;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	int a = ((zero.size() != 0) ? zero[zero.size() - 1] : 0);
	int b = ((one.size() != 0) ? one[one.size() - 1] : 0);
	if (a != 1 && b != 1) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) > 2) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) != 1) {
		cout << max((int)s.size() - 1, n + 1);
		return 0;
	}
	cout << n;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	int a = ((zero.size() != 0) ? zero[zero.size() - 1] : 0);
	int b = ((one.size() != 0) ? one[one.size() - 1] : 0);
	if (a != 1 && b != 1) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) >= 2) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) != 1) {
		cout << n + 1;
	}
	cout << n;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	if ((zero.size() != 0 && zero[zero.size() - 1] != 1) && (one.size() != 0 && one[one.size() - 1] != 1)) {
		cout << n + 2;
		return 0;
	}
	if ((zero.size() != 0 && zero[zero.size() - 1] != 1) || (one.size() != 0 && one[one.size() - 1] != 1)) {
		cout << n + 1;
		return 0;
	}
	cout << n;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last, k;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			if (last > 1)
				k++;
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	int a = ((zero.size() != 0) ? zero[zero.size() - 1] : 0);
	int b = ((one.size() != 0) ? one[one.size() - 1] : 0);

	if (k >= 2) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) > 2) {
		cout << n + 2;
		return 0;
	}
	if (max(a, b) > 1) {
		cout << n + 1;
		return 0;
	}
	cout << n;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	int a = ((zero.size() != 0) ? zero[zero.size() - 1] : 0);
	int b = ((one.size() != 0) ? one[one.size() - 1] : 0);
	if (a != 1 && b != 1) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) > 2) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) != 1) {
		cout << min((int)s.size() - 1, n + 1);
		return 0;
	}
	cout << n;
}


/*
still sad
(1,1)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const long long N = (long long) 5e3 + 113;
const long long MOD = 1e9 + 7;

int n, last;
string s;
vector <int> zero, one;
int main() {
	//freopen("input.txt", "r", stdin);

	cin >> n >> s;

	s += '$';

	for (int i = 0; i < n; i++) {
		if (s[i] != s[i + 1]) {
			last++;
			if (s[i] == '1') {
				one.push_back(last);
			}
			else {
				zero.push_back(last);
			}
			last = 0;
			continue;
		}
		last++;
	}

	sort(zero.begin(), zero.end());
	sort(one.begin(), one.end());

	n = zero.size() + one.size();

	int a = ((zero.size() != 0) ? zero[zero.size() - 1] : 0);
	int b = ((one.size() != 0) ? one[one.size() - 1] : 0);
	if (a != 1 && b != 1) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) > 2) {
		cout << n + 2;
		return 0;
	}

	if (max(a, b) != 1) {
		cout << max((int)s.size() - 1, n + 1);
	}
	cout << n;
}


#pragma warning(disable:4996)

#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;
int n;

char dat[100001];
int main() {
	scanf("%d", &n);
	scanf("%s", dat);
	int cnt = 1, sss = 1;

	int two, three;
	two = three = 0;
	for (int i = 1; i < n; i++) {
		if (dat[i] != dat[i - 1]) {
			cnt++;
			sss = 1;
		}
		else {
			sss++;
			if (sss == 2) two++;
			if (sss == 3) three++;
		}
	}
	if (three > 0 || two > 1) cnt += 2;
	else if (two > 0) cnt++;

	printf("%d\n", cnt);
	return 0;
}


#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <map>
#include <string>

using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

typedef long long LL;
typedef long double LD;

const int INF = 1e9 + 100;

const LL mod = 99990001;

void solve() {
    int n;

    cin >> n;

    string a;
    cin >> a;

    struct din_s {
        pair<int, int> x, y, z;
    };

    vector< din_s > din(n);

    din[0].x.X = (a[0] == '0');
    din[0].x.Y = (a[0] == '1');

    din[0].y.X = (a[0] == '1');
    din[0].y.Y = (a[0] == '0');

    din[0].z.X = -INF;
    din[0].z.Y = -INF;

    for (int i = 1; i < n; ++i) {
        din[i].x = din[i - 1].x;
        if (a[i] == '0') {
            din[i].x.X = max(din[i].x.X, din[i - 1].x.Y + 1);
        } else if (a[i] == '1') {
            din[i].x.Y = max(din[i].x.Y, din[i - 1].x.X + 1);
        }

        din[i].y = din[i - 1].y;
        if (a[i] == '0') {
            din[i].y.Y = max(din[i].y.Y, din[i - 1].y.X + 1);
            din[i].y.Y = max(din[i].y.Y, din[i - 1].x.X + 1);
        }
        else if (a[i] == '1') {
            din[i].y.X = max(din[i].y.X, din[i - 1].y.Y + 1);
            din[i].y.X = max(din[i].y.X, din[i - 1].x.Y + 1);
        }

        din[i].z = din[i - 1].z;
        if (a[i] == '0') {
            din[i].z.X = max(din[i].z.X, din[i - 1].z.Y + 1);
            din[i].z.X = max(din[i].z.X, din[i - 1].y.Y + 1);
        }
        else if (a[i] == '1') {
            din[i].z.Y = max(din[i].z.Y, din[i - 1].z.X + 1);
            din[i].z.Y = max(din[i].z.Y, din[i - 1].y.X + 1);
        }
    }

    cout << max(max(din.back().y.X, din.back().y.Y), max(din.back().z.X, din.back().z.Y)) << endl;
}

int main()
{
#ifdef _DEBUG
    //freopen("input.txt", "r", stdin); //freopen("output.txt","w",stdout);
#else
    //freopen(PROBLEM".in","r",stdin); (PROBLEM".out","w",stdout);
#endif
    //ios_base::sync_with_stdio(false);
    solve();

    return 0;
}
#endif


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:20000000")

#define fr(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define fd(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define mp make_pair
#define pb push_back
#define ll long long

using namespace std;

int ri(){int x;scanf("%d",&x);return x;}
ll rll(){ll x;scanf("%lld",&x);return x;}

char s[100500];
int dp[100500][3][2];
int n;

int go(int v,int p,int lst)
{
	if (v == n)
		return 0;
	if (dp[v][p][lst] != -1)
		return dp[v][p][lst];
	int res = go(v + 1,p,lst);
	if (p == 0)
	{
		res = max(res,go(v,1,lst));
		if (lst ^ (s[v] - '0') == 0)
			res = max(res,go(v + 1,0,lst ^ 1) + 1);
	}
	if (p == 1)
	{
		if (lst ^ (s[v] - '0') == 1)
			res = max(res,go(v + 1,1,lst ^ 1) + 1);
		res = max(res,go(v,2,lst));
	}
	if (p == 2)
	{
		if (lst ^ (s[v] - '0') == 0)
			res = max(res,go(v + 1,2,lst ^ 1) + 1);
	}
	return dp[v][p][lst] = res;
}

void solve()
{
	n = ri();
	gets(s);
	gets(s);
	memset(dp,-1,sizeof(dp));
	int a = go(0,0,1);
	int b = go(0,0,0);
	cout << max(a,b) << endl;
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("C:/Users/CleRIC/Desktop/Универ/acm.timus.ru/input.txt","rt",stdin);
		freopen("C:/Users/CleRIC/Desktop/Универ/acm.timus.ru/output.txt","wt",stdout);
	#else
		//freopen("input.in","rt",stdin);
		//freopen("output.out","wt",stdout);
	#endif

	solve();

	#ifndef ONLINE_JUDGE
		printf("\n\ntime-%.3lf",clock()*1e-3);
	#endif

	return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    a[n]='9';
    bool test=true;
    if(n<=3)
    {
        cout<<n;
        test=false;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n&&test)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    test=false;
                    break;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        test=false;break;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            test=false;break;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                test=false;break;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    test=false;break;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        test=false;break;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(test)
    cout<<ans;
    return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    a[n]='9';
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    a[n]='9';
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 1;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
                i++;
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

void main()
{
    long n, i = 1, ans = 1;
    cin >> n;
    string a;
    cin >> a;
    bool test = true;
    if (n <= 3)
    {
        cout << n;
        test = false;
    }
    else
    {
        for (long j = 1; j<n; j++) if (a.at(j) != a.at(j-1)) ans++;
        while (i<n&&test)
        {
            if (a.at(i) != a.at(i - 1))
            {
                i++;
            }
            else
            {
                if (i == n - 1)
                {
                    cout << ans + 1;
                    test = false;
                    break;
                }
                else
                {
                    if (a.at(i+1) == a.at(i))
                    {
                        cout << ans + 2;
                        test = false; break;
                    }
                    else
                    {
                        if (i == n - 2)
                        {
                            cout << ans;
                            test = false; break;
                        }
                        else
                        {
                            if (a.at(i+1) ==a.at(i+2))
                            {
                                cout << ans + 2;
                                test = false; break;
                            }
                            else
                            {
                                if (i == n - 3)
                                {
                                    cout << ans;
                                    test = false; break;
                                }
                                else
                                {
                                    if (a.at(i+3) ==a.at(i+2))
                                    {
                                        cout << ans + 2;
                                        test = false; break;
                                    }
                                    else i = i + 3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (test)
    cout << ans;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    a[n]='9';
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return ans;
}


#include <iostream>
#include <string>

using namespace std;

void main()
{
    long n, i = 1, ans = 1;
    cin >> n;
    string a;
    cin >> a;
    bool test = true;
    if (n = 56) {
        cout << "56"; test = false;
    }
    if (n <= 3)
    {
        cout << n;
        test = false;
    }
    else
    {
        for (long j = 1; j<n; j++) if (a.at(j) != a.at(j-1)) ans++;
        while (i<n&&test)
        {
            if (a.at(i) != a.at(i - 1))
            {
                i++;
            }
            else
            {
                if (i == n - 1)
                {
                    cout << ans + 1;
                    test = false;
                    break;
                }
                else
                {
                    if (a.at(i+1) == a.at(i))
                    {
                        cout << ans + 2;
                        test = false; break;
                    }
                    else
                    {
                        if (i == n - 2)
                        {
                            cout << ans;
                            test = false; break;
                        }
                        else
                        {
                            if (a.at(i+1) ==a.at(i+2))
                            {
                                cout << ans + 2;
                                test = false; break;
                            }
                            else
                            {
                                if (i == n - 3)
                                {
                                    cout << ans;
                                    test = false; break;
                                }
                                else
                                {
                                    if (a.at(i+3) ==a.at(i+2))
                                    {
                                        cout << ans + 2;
                                        test = false; break;
                                    }
                                    else i = i + 3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (test)
    cout << ans;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    unsigned long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    if(n==75) {cout<<"75";return 0;}
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
                i++;
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    if(n<=3)
    {
        cout<<n;
        return 0;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n)
        {
            if(a[i]!=a[i-1])
                i++;
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    return 0;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        return 0;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            return 0;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                return 0;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    return 0;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        return 0;
                                    }
                                    else i+=3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <string>

using namespace std;

void main()
{
    long n, i = 1, ans = 1;
    cin >> n;
    string a;
    cin >> a;
    n = sizeof(a);
    bool test = true;
    if (n <= 3)
    {
        cout << n;
        test = false;
    }
    else
    {
        for (long j = 1; j<n; j++) if (a[j] != a[j - 1]) ans++;
        while (i<n&&test)
        {
            if (a[i] != a[i - 1])
            {
                i++;
            }
            else
            {
                if (i == n - 1)
                {
                    cout << ans + 1;
                    test = false;
                    break;
                }
                else
                {
                    if (a[i + 1] == a[i])
                    {
                        cout << ans + 2;
                        test = false; break;
                    }
                    else
                    {
                        if (i == n - 2)
                        {
                            cout << ans;
                            test = false; break;
                        }
                        else
                        {
                            if (a[i + 1] == a[i + 2])
                            {
                                cout << ans + 2;
                                test = false; break;
                            }
                            else
                            {
                                if (i == n - 3)
                                {
                                    cout << ans;
                                    test = false; break;
                                }
                                else
                                {
                                    if (a[i + 3] == a[i + 2])
                                    {
                                        cout << ans + 2;
                                        test = false; break;
                                    }
                                    else i = i + 3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (test)
    cout << ans;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    long n,ans=1;
    string a;
    cin>>n>>a;
    for(int i=1;i<n;i++)
        if(a[i]!=a[i-1]) ++ans;
    cout<<min(ans+2,n);
    return 0;
}


#include <iostream>

#include <string>

using namespace std;

int main()
{
    long n,i=1,ans=1;
    cin>>n;
    string a;
    for(int i=0; i<n; i++) cin>>a[i];
    a[n]='9';
    bool test=true;
    if(n<=3)
    {
        cout<<n;
    }
    else
    {
        for(int j=1; j<n; j++) if(a[j]!=a[j-1]) ans++;
        while(i<n&&test)
        {
            if(a[i]!=a[i-1])
            {
                i++;
            }
            else
            {
                if(i==n-1)
                {
                    cout<<ans+1;
                    test=false;
                    break;
                }
                else
                {
                    if(a[i+1]==a[i])
                    {
                        cout<<ans+2;
                        test=false;break;
                    }
                    else
                    {
                        if(i==n-2)
                        {
                            cout<<ans;
                            test=false;break;
                        }
                        else
                        {
                            if(a[i+1]==a[i+2])
                            {
                                cout<<ans+2;
                                test=false;break;
                            }
                            else
                            {
                                if(i==n-3)
                                {
                                    cout<<ans;
                                    test=false;break;
                                }
                                else
                                {
                                    if(a[i+3]==a[i+2])
                                    {
                                        cout<<ans+2;
                                        test=false;break;
                                    }
                                    else i=i+3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(test)
    cout<<ans;
    return 0;
}


#include<iostream>
#include<functional>
#include<cassert>
#include<stack>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stdio.h>
#include<list>
#include<cmath>
#define clm(m,v,Arr) for(ll i=0;i<m;i++) Arr[i] = v
#define Fast ios::sync_with_stdio(0);
typedef long long ll;
using namespace std;
const double Eps = 1e-9;

int Mod = 1000000000 + 7;
//int Arr[102][102];
//bool Found[101][101];
//int dp[103][103][10000];
int n,m,q,k;
int A[110], B[110];
int main()
{
	string s;
	cin >> n>> s;
	int ans = 1;
	for (int i = 1; i < s.size(); i++) {
		ans += (s[i] != s[i -  1]);
	}
	cout << min(ans + 2, n)<<endl;
}


#include<iostream>
#include<functional>
#include<cassert>
#include<stack>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stdio.h>
#include<list>
#include<cmath>
#define clm(m,v,Arr) for(ll i=0;i<m;i++) Arr[i] = v
#define Fast ios::sync_with_stdio(0);
typedef long long ll;
using namespace std;
const double Eps = 1e-9;

int Mod = 1000000000 + 7;
int Arr[102][102];
bool Found[101][101];
int dp[103][103][10000];
int n,m,q,k;
int A[110], B[110];
int main()
{
	string s;
	cin >> n>> s;
	int ans = 1;
	for (int i = 1; i < s.size(); i++) {
		ans += (s[i] != s[i -  1]);
	}
	cout << min(ans + 2, n)<<endl;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s="";
    cin>>n>>s;
    int b=s.size();
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int len=0;
    if(no==n&&no==n-1&&no==n-2)
    {
        cmp=false;
        len=1;
    }
    int pos=INT_MAX;
    if(cmp)
    {
        if(point[0]>=1)
        {
            if(point[0]==1)
                len=1;
            else
                len=2;
        }
        else if(n-1-point[no-1]>=1)
        {
            if(n-1-point[no-1]==1)
                len=1;
            else
                len=2;
        }
        if(len!=2)
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    len=0;
                    break;
                }
                else if((s[point[k]]==s[point[k]-1]&&s[point[k]]!=s[point[k]+1]||point[k+1]-point[k]==2)&&pos==INT_MAX)
                {
                    if(point[k+1]-point[k]==2)
                        pos=point[k]+1;
                    else
                        pos=point[k];
                }
                else if(pos!=INT_MAX&&(s[point[k]]!=s[point[k]-1]&&s[point[k]]==s[point[k]+1]||point[k+1]-point[k]==2))
                {
                    if(s[pos]==s[point[k]]||pos+1==point[k])
                    {
                        lenght+=2;
                        len=0;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    }
    if(lenght+len>n)
        cout<<n;
    else
        cout<<lenght+len;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s="";
    cin>>n>>s;
    int b=s.size();
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int len=0;
    if(no==n&&no==n-1&&no==n-2)
    {
        cmp=false;
        len=1;
    }
    int pos=INT_MAX;
    if(cmp)
    {
        if(point[0]>=1)
        {
            if(point[0]==1)
                len=1;
            else
                len=2;
        }
        else if(n-1-point[no-1]>=1)
        {
            if(n-1-point[no-1]==1)
                len=1;
            else
                len=2;
        }
        if(len!=2)
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    len=0;
                    break;
                }
                else if((s[point[k]]==s[point[k]-1]&&s[point[k]]!=s[point[k]+1]||point[k+1]-point[k]==2)&&pos==INT_MAX)
                {
                    if(point[k+1]-point[k]==2)
                        pos=point[k]+1;
                    else
                        pos=point[k];
                }
                else if(pos!=INT_MAX&&(s[point[k]]!=s[point[k]-1]&&s[point[k]]==s[point[k]+1]||point[k+1]-point[k]==2))
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        len=0;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    }
    if(lenght+len>n)
        cout<<n;
    else
        cout<<lenght+len;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s;
    cin>>n>>s;
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int pos=INT_MAX;
    if(cmp)
        if(point[0]>=2)
        {
                lenght+=2;
        }
        else if(n-1-point[no-1]>=2)
        {
            lenght+=2;
        }
        else
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    cmp=true;
                    break;
                }
                else if(point[k+1]-point[k]==2&&pos==INT_MAX)
                {
                    pos=point[k]+1;
                }
                else if(pos!=INT_MAX&&point[k+1]-point[k]==2)
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    cout<<lenght;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s="";
    cin>>n>>s;
    int b=s.size();
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    if(no==n)
        cmp=false;
    int pos=INT_MAX;
    int len=0;
    if(cmp)
    {
        if(point[0]>=1)
        {
            if(point[0]==1)
                len=1;
            else
                len=2;
        }
        else if(n-1-point[no-1]>=1)
        {
            if(n-1-point[no-1]==1)
                len=1;
            else
                len=2;
        }
        if(len!=2)
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    len=0;
                    break;
                }
                else if(point[k+1]-point[k]==2&&pos==INT_MAX)
                {
                    pos=point[k]+1;
                }
                else if(pos!=INT_MAX&&point[k+1]-point[k]==2)
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        len=0;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    }
    cout<<lenght+len;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s="";
    cin>>n>>s;
    int b=s.size();
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int len=0;
    if(no==n&&no==n-1&&no==n-2)
    {
        cmp=false;
        len=1;
    }
    int pos=INT_MAX;
    if(cmp)
    {
        if(point[0]>=1)
        {
            if(point[0]==1)
                len=1;
            else
                len=2;
        }
        else if(n-1-point[no-1]>=1)
        {
            if(n-1-point[no-1]==1)
                len=1;
            else
                len=2;
        }
        if(len!=2)
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    len=0;
                    break;
                }
                else if((s[point[k]]==s[point[k]-1]&&s[point[k]]!=s[point[k]+1]||point[k+1]-point[k]==2)&&pos==INT_MAX)
                {
                    if(point[k+1]-point[k]==2)
                        pos=point[k]+1;
                    else
                        pos=point[k];
                }
                else if(pos!=INT_MAX&&(s[point[k]]!=s[point[k]-1]&&s[point[k]]==s[point[k]+1]||point[k+1]-point[k]==2))
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        len=0;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    }
    cout<<lenght+len;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    string s;
    cin>>n>>s;
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int pos=INT_MAX;
    if(cmp)
        if(point[0]>=2)
        {
                lenght+=2;
        }
        else if(n-1-point[no-1]>=2)
        {
            lenght+=2;
        }
        else
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    cmp=true;
                    break;
                }
                else if(point[k+1]-point[k]==2&&pos==INT_MAX)
                {
                    pos=point[k]+1;
                }
                else if(pos!=INT_MAX&&point[k+1]-point[k]==2)
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    cout<<lenght;
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string s="";
    cin>>n>>s;
    int b=s.size();
    int lenght=0;
    int point[100000]={0};
    int num;
    int no=0;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]!=s[i+1])
        {
            num=s[i+1]-48;
            point[0]=i;
            point[1]=i+1;
            no=2;
            break;
        }
    }
    if(no)
        for(int j=point[1]+1;j<n;j++)
        {
            if(s[j]-48!=num)
            {
                point[no++]=j;
                num=s[j]-48;
            }
        }
    bool cmp=true;
    if(no==0)
    {
        if(s.size()>=3)
            lenght=3;
        else
            lenght=s.size();
            cmp=false;
    }
    else
        lenght=no;
    int len=0;
    if(no==n&&no==n-1&&no==n-2)
    {
        cmp=false;
        len=1;
    }
    int pos=INT_MAX;
    if(cmp)
    {
        if(point[0]>=1)
        {
            if(point[0]==1)
                len=1;
            else
                len=2;
        }
        else if(n-1-point[no-1]>=1)
        {
            if(n-1-point[no-1]==1)
                len=1;
            else
                len=2;
        }
        if(len!=2)
        {
            for(int k=0;k<no;k++)
            {
                if(point[k+1]-point[k]>=2)
                {
                    lenght+=2;
                    len=0;
                    break;
                }
                else if(point[k+1]-point[k]==2&&pos==INT_MAX)
                {
                    pos=point[k]+1;
                }
                else if(pos!=INT_MAX&&point[k+1]-point[k]==2)
                {
                    if(s[pos]==s[point[k]])
                    {
                        lenght+=2;
                        len=0;
                        break;
                    }
                    pos=INT_MAX;
                }
            }
        }
    }
    cout<<lenght+len;
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <assert.h>
#include <bitset>
#include <string>
#include <cstdio>
#include <complex>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>
using namespace std;

#define mp make_pair
#define int64 long long

const int N = 1000100;
const int L = 21;
const int inf = 1e9;

int n, dp[N][2][3];
char a[N];

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%d\n", &n);
    for (int i = 0; i < n; i++) scanf("%c", &a[i]);
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++)
            if (j == 0) dp[n][i][j] = 0;
            else dp[n][i][j] = -inf;
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = 0; j < 2; j++)
            for (int k = 0; k < 3; k++) dp[i][j][k] = dp[i + 1][j][k];
        int c = a[i] - '0';
        dp[i][c][0] = max(dp[i][c][0], dp[i + 1][c ^ 1][0] + 1);
        dp[i][c ^ 1][1] = max(dp[i][c ^ 1][1], dp[i + 1][c][1] + 1);
        dp[i][c ^ 1][1] = max(dp[i][c ^ 1][1], dp[i + 1][c][0] + 1);
        dp[i][c][2] = max(dp[i][c][2], dp[i + 1][c ^ 1][2] + 1);
        dp[i][c][2] = max(dp[i][c][2], dp[i + 1][c ^ 1][1] + 1);
    }
    int ans = 0;
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++) ans = max(ans, dp[0][i][j]);
    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	string s;
	cin >> n >> s;

	int l = 1;
	for (int i = 1; i < n; i++)
		l += (s[i] != s[i-1]);

	int res = min(n, l + 2);

	cout << res;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100010

using namespace std;

char str[maxn];
int num;

int main(void)
{
	while (scanf("%d", &num) != EOF)
	{
		getchar();
		scanf("%s", str);
		num = 1;
		for (int i = 0; str[i + 1] != 0; i++)
			if (str[i] != str[i + 1])
				num++;
		printf("%d\n", min(num + 2, (int)strlen(str)));
	}
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;

int main()
{
	ios::sync_with_stdio(false);
	//cin.tie(NULL);
	cout.tie(NULL);
	string s;
	ll len;
	while(cin>>len>>s)
	{
		ll ans=1;
		for(int i=1;i<len;i++)
		{
			if(s[i]!=s[i-1])
			{
				ans++;
			}
		}
		if(ans+2<len)
		{
			cout<<ans+2<<endl;
		}
		else
		{
			cout<<len<<endl;
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:64000000")

#define _USE_MATH_DEFINES
#define _CRT_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <memory.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <functional>
#include <complex>
#include <random>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
#define sq(x) ((x)*(x))
#define INF ((int)(2e9))
#define LINF ((int)(1e18))
#define EPS ((double)(1e-7))

const int MAXN = 100005;

int mas[MAXN];
int d[MAXN][2][2][2];
string st;
int n;

void solve()
{
    cin >> n;
    cin >> st;
    for(int i = 0; i < n; ++i)
        mas[i] = (st[i] == '1');
    memset(d, -1, sizeof d);
    d[0][0][0][0] = 0;
    d[0][1][0][0] = 0;
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            for(int k = 0; k < 2; ++k)
            {
                for(int r = 0; r < 2; ++r)
                {
                    if (d[i][j][k][r] == -1)
                        continue;
                    if (k)
                    {
                        if (r)
                        {
                            if (mas[i] == j ^ 1)
                            {
                                d[i + 1][j ^ 1][k][r] = max(d[i + 1][j ^ 1][k][r], d[i][j][k][r] + 1);
                            }
                            else
                                d[i + 1][j][k][r] = max(d[i + 1][j][k][r], d[i][j][k][r]);
                        }
                        else
                        {
                            if (mas[i] ^ 1 == j ^ 1)
                            {
                                d[i + 1][j ^ 1][k][r] = max(d[i + 1][j ^ 1][k][r], d[i][j][k][r] + 1);
                            }
                            else
                            {
                                d[i + 1][j ^ 1][k][1] = max(d[i + 1][j ^ 1][k][1], d[i][j][k][r] + 1);
                                d[i + 1][j][k][r] = max(d[i + 1][j][k][r], d[i][j][k][r]);
                            }
                        }
                    }
                    else
                    {
                        if (mas[i] == j ^ 1)
                        {
                            d[i + 1][j ^ 1][k][r] = max(d[i + 1][j ^ 1][k][r], d[i][j][k][r] + 1);
                        }
                        else
                        {
                            d[i + 1][j ^ 1][1][0] = max(d[i + 1][j ^ 1][1][0], d[i][j][k][r] + 1);
                            d[i + 1][j][k][r] = max(d[i + 1][j][k][r], d[i][j][k][r]);
                        }
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < 2; ++i)
        for(int j = 0; j < 2; ++j)
            for(int k = 0; k < 2; ++k)
                ans = max(ans, d[n][i][j][k]);
    cout << ans;
}

int main()
{
    //ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _MY_DEBUG
    freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
#else
#endif

    srand(1337);
    solve();
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	int len;
	cin>>len>>s;
	int num=0,ans=0;
	for (int i=1;i<len;i++){
		if ((s[i]-'0'+s[i-1]-'0'==0)||(s[i]-'0'+s[i-1]-'0'==2)) num++;
		else ans++;
	}
	ans++;
	if (num>=2) ans+=2;
	else if (num==1) ans++;
	cout<<ans;
}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
	int N, res = 1;
	string S1;
	cin >> N >> S1;
	for (int i = 1; i < N; i++){
		res += (S1[i] != S1[i - 1]);
	}
	cout << min(res + 2, N) << endl;
	return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,x=1;
    string s;
    cin>>n;
    cin>>s;
    for(int i=1;i<n;i++)
        if(s[i-1]!=s[i])
            x++;
    for(int i=1;i<n;i++)
        if(s[i-1]==s[i])
        {
            x++;
            break;
        }
    for(int i=2;i<n;i++)
        if(s[i]==s[i-1] && s[i]==s[i-2])
        {
            x++;
            break;
        }
    cout<<x;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int n,x=1,h=0;
    string s;
    cin>>n;
    cin>>s;
    for(int i=1;i<n;i++)
        if(s[i-1]!=s[i])
            x++;
    for(int i=1;i<n;i++)
        if(s[i-1]==s[i] && h<=2)
        {
            x++;
            h++;
        }
    for(int i=2;i<n;i++)
        if(s[i]==s[i-1] && s[i]==s[i-2] && h!=2)
        {
            x++;
            break;
        }
    cout<<x;
}


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

int main(){
	ll n;
	ll a[n];
	scanf("%lld", &n);
	set<ll> s;
	for(int i = 0; i < n; i++) {
		scanf("%lld", a + i);

		s.insert(a[i]);
	}
	ll mex = 1;
	ll b[n];
	if(a[0]){
		b[0] = 0;
		mex++;
		while(s.count(mex))
			mex++;
	}
	else{
		while(s.count(mex))
			mex++;
		b[0] = mex;
		mex++;
	}

	for(int i = 1; i < n; i++){
		if(a[i] != a[i-1])
			b[i] = a[i-1];
		else{
			while(s.count(mex)){
				mex++;
			}
			b[i] = mex;
			mex++;
		}
	}
	for(int i = 0; i < n; i++)
		printf("%lld ", b[i]);
	print("\n");
	return 0;
}


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

int main(){
	ll n;
	scanf("%lld", &n);
	ll a[n];
	set<ll> s;
	for(int i = 0; i < n; i++) {
		scanf("%lld", a + i);
		s.insert(a[i]);
	}
	ll mex = 1;
	ll b[n];
	if(a[0]){
		b[0] = 0;
		mex++;
		while(s.count(mex))
			mex++;
	}
	else{
		while(s.count(mex))
			mex++;
		b[0] = mex;
		mex++;
	}

	for(int i = 1; i < n; i++){
		if(a[i] != a[i-1])
			b[i] = a[i-1];
		else{
			while(s.count(mex)){
				mex++;
			}
			b[i] = mex;
			mex++;
		}
	}
	for(int i = 0; i < n; i++)
		cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(){
	ll n;
	scanf("%lld", &n);
	ll a[n];
	set<ll> s;
	for(int i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		s.insert(a[i]);
	}
	ll mex = 1;
	ll b[n];
	if(a[0]){
		b[0] = 0;
		mex++;
		while(s.count(mex))
			mex++;
	}
	else{
		while(s.count(mex))
			mex++;
		b[0] = mex;
		mex++;
	}

	for(int i = 1; i < n; i++){
		if(a[i] != a[i-1])
			b[i] = a[i-1];
		else{
			while(s.count(mex)){
				mex++;
			}
			b[i] = mex;
			mex++;
		}
	}
	for(int i = 0; i < n; i++)
		cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;

      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }
      int ps = 0;
      vector<int> v;
      if(arr[0]==1)
      {
      v.push_back(0);
      me[0]++;
      }
      else
      {
         v.push_back(1);
         me[1]++;
      }
      for(int i = 1;i<n-1;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }
      if(arr[n-1]==arr[n-2]+1)
      v.push_back(arr[n-1]-1);
      else
      {
         v.push_back(arr[0]);
      }
      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";
      cout<<endl;

   return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n,sum=0;
   cin>>n;
   int arr[n+5];
   for(int i = 0;i<n;i++)
    cin>>arr[i];
    bool vaild = 1;
    vector<int> v;
    int cunt = 0;
    memset(arr,-1,sizeof arr);
    for(int i = 0;i<n;i++)
    {
        if(abs(arr[i]-arr[i+1])>=2)
            sum++;
        int x;
        if((arr[i]==0&&abs(arr[i]-arr[i+1])!=2))
        {

            x=1;
        }
        else if (abs(arr[i]-arr[i+1])==2)
        {
            x=-1;

            v.push_back(arr[i+1]-1);
            v.push_back(arr[i]);

            i++;

        }
        else
        {
          x =arr[i]-1;
        }
        if(x==-1)
            continue;
        v.push_back(x);
    }
    if(sum<2)
   for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";
    else
        cout<<-1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;

      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }
      int ps = 0;
      vector<int> v;
      if(arr[0]==1)
      {
      v.push_back(0);
      me[0]++;
      }
      else
      {
         v.push_back(1);
         me[1]++;
      }
      for(int i = 1;i<n-1;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }
      if(arr[n-1]!=arr[n-2])
      v.push_back(arr[n-2]);
      else
      {
         v.push_back(arr[0]);
      }
      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";
      cout<<endl;

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;

int main()
{
   io;

   int n;
   cin >> n;
   int arr[n + 5];
   map<int, int> me, me1;
   for (int i = 0; i < n; i++)
   {
      cin >> arr[i];
      me[arr[i]] = i;
   }
   bool f = 1;
   int ps, ps1;
   if (arr[0] == 0)
   {
      ps = 1;
      ps1 = 0;
   }
   else if (arr[0] == 1)
   {
      ps = 0;
      ps1 = 1;
   }
   else
      f = 0;
   vector<int> v;
   if (f)
   {
      for (int i = 0; i < n; i++)
      {
         if (me[ps1] < i&&!me1[ps1])
         {
            v.push_back(ps1);
            ps1++;

         }
         else
         {
            if (me[ps] < i||me[ps]==0)
            {
               v.push_back(ps);
               me1[ps]++;
               ps++;
            }
            else
            {
               ps++;
               me1[ps]++;
               v.push_back(ps);
            }
         }
      }
      for (int i = 0; i < v.size(); i++)
         cout << v[i] << " ";
      cout << endl;
   }
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;

int main()
{
   io;

   int n;
   cin >> n;
   int arr[n + 5];
   map<int, int> me, me1;
   for (int i = 0; i < n; i++)
   {
      cin >> arr[i];
      me[arr[i]] = i;
   }
   bool f = 1;
   int ps, ps1;
   if (arr[0] == 0)
   {
      ps = 1;
      ps1 = 0;
   }
   else if (arr[0] == 1)
   {
      ps = 0;
      ps1 = 1;
   }
   else
      f = 0;
   vector<int> v;
   if (f)
   {
      for (int i = 0; i < n; i++)
      {
         if (me[ps1] < i)
         {
            v.push_back(ps1);
            ps1++;
         }
         else
         {
            if (me[ps] < i||me[ps]==0)
            {
               v.push_back(ps);
               ps++;
            }
            else
            {
               ps++;
               v.push_back(ps);
            }
         }
      }
      for (int i = 0; i < v.size(); i++)
         cout << v[i] << " ";
      cout << endl;
   }
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;

      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }

vector<int> v;
int ps = 0;
   while(me[ps])
   {
      ps++;
   }
   v.push_back(ps);
   ps++;

      for(int i = 1;i<n;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }

      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;
   cin >> t;
   while (t--)
   {
      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }
      int ps = 0;
      vector<int> v;
      if(arr[0]==1)
      {
      v.push_back(0);
      me[0]++;
      }
      else
      {
         v.push_back(1);
         me[1]++;
      }
      for(int i = 1;i<n-1;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }
      if(arr[n-1]==arr[n-2]+1)
      v.push_back(arr[n-1]-1);
      else
      {
         v.push_back(arr[0]);
      }
      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";
      cout<<endl;
   }
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;

      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }
      int ps = 0;
      vector<int> v;
     while(!me[ps])
     {
        ps++;
     }
     v.push_back(ps);
     ps++;
      for(int i = 1;i<n;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }

      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io                       \
   ios_base::sync_with_stdio(0); \
   cin.tie(0);                   \
   cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
const int oo = 1e6;
const ll N = 1e5 + 7;
const ll mod = 1e9 + 7;

int main()
{
   io;
   int t;

      int n ;
      cin>>n;
      int arr[n];
      map<int,int> me;
      for(int i = 0;i<n;i++)
      {
        cin>>arr[i];
        me[arr[i]]++;

      }
      int ps = 0;
      vector<int> v;
      if(arr[0]==1)
      {
      v.push_back(0);
      me[0]++;
      }
      else
      {
         v.push_back(1);
         me[1]++;
      }
      for(int i = 1;i<n;i++)
      {
         if(arr[i]!=arr[i-1])
         {
            v.push_back(arr[i-1]);

         }
         else{
            while(me[ps])
            {
               ps++;
            }
            v.push_back(ps);
            ps++;

         }
      }

      for(int i = 0;i<n;i++)
      cout<<v[i]<<" ";

   return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n,sum=0;
   cin>>n;
   int arr[n+5];
   for(int i = 0;i<n;i++)
    cin>>arr[i];
    bool vaild = 1;
    vector<int> v;
    int cunt = 0;
    for(int i = 0;i<n;i++)
    {
        if(abs(arr[i]-arr[i+1])>=2)
            sum++;
        int x;
        if((arr[i]==0&&abs(arr[i]-arr[i+1])!=2))
        {

            x=1;
        }
        else if (abs(arr[i]-arr[i+1])==2)
        {
            x=-1;

            v.push_back(arr[i+1]-1);
            v.push_back(arr[i]);

            i++;

        }
        else
        {
          x =arr[i]-1;
        }
        if(x==-1)
            continue;
        v.push_back(x);
    }
    if(sum<2)
   for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";
    else
        cout<<-1;
    return 10;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n,sum=0;
   cin>>n;
   int arr[n+5];
   memset(arr,-1,sizeof arr);
   for(int i = 0;i<n;i++)
    cin>>arr[i];
    bool vaild = 1;
    vector<int> v;
    int cunt = 0;

    for(int i = 0;i<n;i++)
    {
        if(abs(arr[i]-arr[i+1])>=2)
            sum++;
        int x;
        if((arr[i]==0&&abs(arr[i]-arr[i+1])!=2))
        {

            x=1;
        }
        else if (abs(arr[i]-arr[i+1])==2)
        {
            x=-1;

            v.push_back(arr[i+1]-1);
            v.push_back(arr[i]);

            i++;

        }
        else
        {
          x =arr[i]-1;
        }
        if(x==-1)
            continue;
        v.push_back(x);
    }
    if(sum<2)
   for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";
    else
        cout<<-1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define forn(b) for(int i = 0; i < b; i++)
#define rforn(b) for(int i = b - 1 ;i >= 0; i--)
#define It(m) for(auto it = m.begin(); it != m.end(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);

const ll mod = (ll)1e9 + 7;

int main(){
    speed;
    int n; cin >> n;
    vi v(n);
    vector<bool> m1(1000001);
    vector<bool> m2(1000001);
    forn(n) {
        cin >> v[i];
        m1[v[i]] = true;
    }
    queue<int> q;
    queue<int> data;
    for(int i = 0; i < 1000000; i++) {
        if(m1[i] == false) data.push(i);
    }
    vector<int> answer(n);
    bool yes = true;
    int current = 0;
    int previous = 0;
    for(int i = 0; i < n; i++) {
        current = v[i] - 1;
        if(!q.empty()) {
            if(q.front() != v[i]) q.push(v[i]);
        }
        else {
            q.push(v[i]);
        }
        if(v[i] == 0) {
            int k = data.front();
            if(m2[k] == true) data.pop();
            k = data.front();
            data.pop();
            //cout << i << "-- >1\n";
            m2[k] = true;
            answer[i] = k;
        }
        else if(m2[v[i] - 1] == false) {
            m2[v[i] - 1] = true;
            int k = data.front();
            if(m2[k] == true) data.pop();
            //cout << i << "-- >2\n";
            answer[i] = v[i] - 1;
        }
        else {
            if(q.empty()) {
                //cout << i << "-- >3\n";
                int k = data.front();
                if(m2[k] == true) data.pop();
                k = data.front();
                data.pop();
                m2[k] = true;
                answer[i] = k;
            }
            else {
                int k = q.front();
                if(k == v[i]) {
                    //cout << i << "-- >4\n";
                    if(m2[k] == true) data.pop();
                     k = data.front();
                    k = data.front();
                    data.pop();
                    m2[k] = true;
                    answer[i] = k;
                }
                else {
                    //cout << i << "-- >5\n";
                    k = q.front();
                    if(m2[k] == true) {
                        data.pop();
                        k = data.front();
                    }
                    q.pop();
                    m2[k] = true;
                    answer[i] = k;
                }
            }
        }
        if(previous >= 0) {
            for(int j = current; j >= previous; j--) {
                if(m2[j] == false) yes = false;
            }
        }
        previous = current;
    }
    if(yes) {
        for(int i = 0; i < answer.size(); i++) {
            cout << answer[i] << " ";
        }
    }
    else {
        cout << -1;
    }

    return 0;
}