#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN = 123456;
void pre();
void post();
ll n;
string s;
void read()
{
cin >> n;
cin >> s;
}
int main()
{
pre();
read();
ll maxans;
ll nowans;
bool now;
// if from 0 start
now = true;
nowans=0;
for(int i=0;i<n;i++)
{
if(now) {if(s[i]=='0'){now = !now;nowans++;}}
else {if(s[i]=='1'){now = !now;nowans++;}}
}
maxans = nowans;
// if from 1 start
now = false;
nowans=0;
for(int i=0;i<n;i++)
{
if(now) {if(s[i]=='0'){now = !now;nowans++;}}
else {if(s[i]=='1'){now = !now;nowans++;}}
}
maxans = max(maxans,nowans);
vector<ll> blocks;
bool flag = s[0]=='0'?false:true;
bool startflag = flag;
blocks.push_back(0);
for(int i=0;i<n;i++)
{
if(flag && s[i]=='1')blocks[blocks.size()-1]++;
else if(!flag && s[i]=='0')blocks[blocks.size()-1]++;
else {flag = !flag;blocks.push_back(1);}
}
ll cntbolshe2=0;
for(int i=0;i<blocks.size();i++)
{
if(blocks[i]>=2)cntbolshe2++;
}
if(cntbolshe2>=2)
{
cout << maxans+2;
return 0;
}
for(int i=0;i<blocks.size();i++)
{
if(i<blocks.size()-1)
if(blocks[i]>=2 && blocks[i+1]>=2)
{
cout << maxans+2;
return 0;
}
if(i<blocks.size()-2)
if(blocks[i]>=2 && blocks[i+2]>=2)
{
cout << maxans+2;
return 0;
}
if(blocks[i]>=3)
{
cout << maxans+2;
return 0;
}
}
for(int i=0;i<blocks.size();i++)
{
if(blocks[i]>=2)
{
cout << maxans+1;
return 0;
}
}
cout << maxans;
/*if(s.find("0011")!=string::npos)
{
cout << maxans+2;
}
else if(s.find("011")!=string::npos || s.find("001")!=string::npos ||
s.find("111")!=string::npos || s.find("000")!=string::npos)
{
cout << maxans+1;
}
else cout << maxans;*/
post();
return 0;
}
void pre()
{
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void post()
{
#ifdef DEBUG
cerr << "Execution time: " << (ld) clock() / CLOCKS_PER_SEC;
#endif
}