#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<numeric>
#include<string>
#include<stack>
#include<queue>
#include<string.h>
#include<array>
#include<climits>
#include<algorithm>
#include<cmath>
using namespace std;
#define ff first
#define int long long
#define ss second
const int mx = 1500000;
#define int long long
const long long inf = 1e18;
#define endl '\n'
const int maxn = 2e5 + 5;
int fi(int a, int b, int c){
set<int> s = {a, b, c};
s.erase(0);
return s.size();
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector<int> g(n);
for(int i = 0; i < n; i++){
if(s[i] == 'M'){
g[i] = 1;
}
else if(s[i] == 'F'){
g[i] = 2;
}
else{
g[i] = 3;
}
}
int new_dp[4][4][4][4];
int dp[4][4][4][4];
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
for(int c = 0; c < 4; c++){
for(int d = 0; d < 4; d++){
dp[a][b][c][d] = -inf;
}
}
}
}
dp[0][0][0][0] = 0;
for(int i = 0; i < n; i++){
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
for(int c = 0; c < 4; c++){
for(int d = 0; d < 4; d++){
new_dp[a][b][c][d] = -inf;
}
}
}
}
int x = g[i];
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
for(int c = 0; c < 4; c++){
for(int d = 0; d < 4; d++){
if(dp[a][b][c][d] == -inf) continue;
int add = fi(a, b, x);
new_dp[b][x][c][d] = max(new_dp[b][x][c][d], dp[a][b][c][d] + add);
int add1 = fi(c, d, x);
new_dp[a][b][d][x] = max(new_dp[a][b][d][x], dp[a][b][c][d] + add1);
}
}
}
}
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
for(int c = 0; c < 4; c++){
for(int d = 0; d < 4; d++){
dp[a][b][c][d] = new_dp[a][b][c][d];
}
}
}
}
}
int mx = 0;
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
for(int c = 0; c < 4; c++){
for(int d = 0; d < 4; d++){
mx = max(mx, dp[a][b][c][d]);
}
}
}
}
cout << mx;
}