#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool getmin(T *a, const T &b) {
  if (b < *a) {
    *a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool getmax(T *a, const T &b) {
  if (b > *a) {
    *a = b;
    return true;
  }
  return false;
}
template <class T>
inline void read(T *a) {
  char c;
  while (isspace(c = getchar())) {
  }
  bool flag = 0;
  if (c == '-')
    flag = 1, *a = 0;
  else
    *a = c - 48;
  while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
  if (flag) *a = -*a;
}
const int mo = 1000000007;
template <class T>
T pow(T a, T b, int c = mo) {
  T res = 1;
  for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)
    if (b & i) res = 1LL * res * a % c;
  return res;
}
const int N = 200000;
int f[N], g[N];
int s[N], a[N][2], b[N][2];
int n;
int main() {
  cin >> n;
  for (int i = (1); i <= (n); ++i) {
    char c;
    while (isspace(c = getchar()))
      ;
    if (c == '0')
      s[i] = 0;
    else
      s[i] = 1;
  }
  f[1] = 1;
  for (int i = (2); i <= (n); ++i) {
    a[i][0] = a[i - 1][0];
    a[i][1] = a[i - 1][1];
    a[i][s[i - 1]] = i - 1;
    f[i] = f[a[i][s[i] ^ 1]] + 1;
  }
  g[n] = 1;
  for (int i = (n - 1); i >= (1); --i) {
    b[i][0] = b[i + 1][0];
    b[i][1] = b[i + 1][1];
    b[i][s[i + 1]] = i + 1;
    g[i] = g[b[i][s[i] ^ 1]] + 1;
  }
  int ans = 0;
  for (int i = (1); i <= (n); ++i)
    getmax(&ans, f[a[i][s[i]]] + g[b[i][s[i]]] + 1);
  ans = f[n];
  cout << min(ans + 2, n) << endl;
}
