#include <bits/stdc++.h>
using namespace std;
int BinarySearch(int *p, int key, int size) {
  int l = 0, r = size - 1;
  if (p[(r - l) / 2] == key)
    return (r - l) / 2;
  else if (p[(r - l) / 2] > key) {
    r = (r - l) / 2 - 1;
  } else
    l = (r - l) / 2 + 1;
}
char change(char p) {
  if (p == '1')
    return '0';
  else
    return '1';
}
int main(int argc, char **argv) {
  int n, otv = 0;
  cin >> n;
  string str;
  char next;
  cin >> str;
  next = str[0];
  for (int i = 0; i < n; i++) {
    if (str[i] == next) {
      otv++;
      next = change(next);
    }
  }
  int counter = 0;
  for (int i = 0, j = 0; (str.find("00", j) != string::npos) ||
                         (str.find("11", i) != string::npos);) {
    if (str.find("11", i) != string::npos) {
      counter++;
      i = str.find("11", i) + 1;
    }
    if (counter == 2) break;
    if (str.find("00", j) != string::npos) {
      counter++;
      j = str.find("00", j) + 1;
    }
    if (counter == 2) break;
  }
  cout << otv + fmin(2, counter);
  return 0;
}
