#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int inf = 1e9 + 7;
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<long long int> > left(n, vector<long long int>(2, 0)),
      right(n, vector<long long int>(2, 0));
  left[0][s[0] - '0'] = 1;
  right[n - 1][s[n - 1] - '0'] = 1;
  long long int i;
  long long int ans = 0;
  vector<long long int> v(n, 1);
  for (i = 1; i < n; i++) {
    long long int xx = s[i] - '0';
    left[i][xx] = left[i - 1][1 - xx] + 1;
    left[i][1 - xx] = left[i - 1][1 - xx];
    if (s[i] != s[i - 1]) {
      v[i] = v[i - 1] + 1;
    }
  }
  for (i = n - 2; i >= 0; i--) {
    long long int xx = s[i] - '0';
    right[i][xx] = right[i + 1][1 - xx] + 1;
    right[i][1 - xx] = right[i + 1][1 - xx];
  }
  ans = max(left[n - 1][0], left[n - 1][1]);
  long long int l = 0;
  for (i = 0; i < n; i++) {
    if (v[i] == 1) l = i;
    if (i + 1 >= n || v[i + 1] == 1) {
      long long int xx = s[i] - '0';
      long long int lxx = s[l] - '0';
      long long int tmp = 0;
      if (l - 1 >= 0) {
        tmp += left[l - 1][lxx];
      }
      if (i + 1 < n) {
        tmp += right[i + 1][xx];
      }
      tmp += i - l + 1;
      ans = max(ans, tmp);
    }
  }
  cout << ans << '\n';
  ;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
