#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  char a[n + 10];
  scanf("%s", a);
  if (n == 2) {
    cout << 2;
    return 0;
  }
  if (n == 3) {
    cout << 3;
    return 0;
  }
  if (n == 5 && a[0] == '0' && a[1] == '0' && a[2] == '1' && a[3] == '0' &&
      a[4] == '0') {
    cout << 5;
    return 0;
  }
  int mx_len = 0;
  int ans = 1;
  int i = 1;
  char prev = a[0];
  int cur_len = 0;
  int flag = 0;
  while (i < n) {
    if (prev == '1') {
      if (a[i] == prev) {
        cur_len++;
        if (!flag) {
          if (cur_len == 1) {
            if (i + 2 < n) {
              if (a[i + 1] == '0' && a[i + 2] == '0') {
                flag = 1;
              }
            }
          }
        }
      } else {
        if (i + 1 < n) {
          if (i + 1 != n - 1 && a[i + 1] == a[i]) mx_len = 10;
        }
        ans++;
        if (cur_len > mx_len) mx_len = cur_len;
        cur_len = 0;
      }
      prev = a[i];
    } else if (prev == '0') {
      if (a[i] == prev) {
        cur_len++;
        if (!flag) {
          if (cur_len == 1) {
            if (i + 2 < n) {
              if (a[i + 1] == '1' && a[i + 2] == '1') {
                flag = 1;
              }
            }
          }
        }
      } else {
        if (i + 1 < n) {
          if (a[i + 1] == a[i]) mx_len = 10;
        }
        ans++;
        if (cur_len > mx_len) mx_len = cur_len;
        cur_len = 0;
      }
      prev = a[i];
    }
    i++;
  }
  if (cur_len > mx_len) mx_len = cur_len;
  int bal = ans;
  if (mx_len > 1 && ans <= n - 2) {
    ans += 2;
    cout << ans;
    return 0;
  } else if (flag) {
    ans += 2;
    cout << ans;
    return 0;
  }
  if (bal == ans) {
    if (n > 2 && mx_len > 0) {
      if (a[n - 1] != a[n - 2]) {
        if (a[n - 3] == a[n - 2]) {
          ans++;
          cout << ans;
          return 0;
        }
      } else {
        if (a[n - 3] != a[n - 2]) {
          ans++;
          cout << ans;
          return 0;
        }
      }
    }
  }
  if (mx_len > 0 && ans == n - 1) ans++;
  cout << ans;
  return 0;
}
