#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
bool prime[100005];
void Sieve(long long int n) {
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
long long int LAS(long long int arr[], long long int n) {
  long long int inc = 1;
  long long int dec = 1;
  for (long long int i = 1; i < n; i++) {
    if (arr[i] > arr[i - 1]) {
      inc = dec + 1;
    } else if (arr[i] < arr[i - 1]) {
      dec = inc + 1;
    }
  }
  return max(inc, dec);
}
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  long long int x = 0, y = 0;
  for (auto i : s) {
    if (i == '1')
      x++;
    else
      y++;
  }
  if (x == 0 || y == 0) {
    if (n > 2)
      cout << 3 << '\n';
    else
      cout << n << '\n';
    return;
  }
  vector<char> f;
  long long int ans = 0;
  long long int zf = 0, of = 0;
  for (long long int i = 0; i < n; i++) {
    long long int z = 0, o = 0;
    while (i < n && f.size() > 0 && s[i] == f.back()) {
      i++;
      if (s[i] == '0')
        z++;
      else
        o++;
    }
    if (z + 1 >= 2) zf++;
    if (o + 1 >= 2) of++;
    if (z + 1 > 2) {
      ans += 2;
    }
    if (o + 1 > 2) ans += 2;
    if (i < n) f.push_back(s[i]);
  }
  long long int size = f.size();
  ans += size;
  if ((zf >= 1 && of >= 1) || (zf >= 2 || of >= 2)) {
    cout << size + 2 << '\n';
  } else if (zf >= 1 || of >= 1) {
    if ((s[0] == s[1] && s[1] == s[2]) ||
        (s[n - 3] == s[n - 2] && s[n - 2] == s[n - 1]))
      cout << size + 2 << '\n';
    else
      cout << size + 1 << '\n';
  } else
    cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
