#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int n; string s;
bool used[1000000];
vector<int>O, I;
bool ok(int t) {
	if (I.size() < t || O.size() < t)return false;
	memset(used, 0, sizeof(used));
	rep(i, t)used[I[i]] = 1, used[O[i]] = 1;
	int p = n - 1;
	rep(i, t) {
		p = min(p - 1, O[i] - 1);
		while (0 <= p && (used[p] || s[p] == 'O'))p--;
		if (p < 0)return false;
	}
	return true;
}
int main() {
	cin >> n >> s;
	for (int i = n - 1; i >= 0; i--) {
		if (s[i] == 'I')I.push_back(i);
		if (s[i] == 'O'&&O.size() < I.size())O.push_back(i);
	}
	int l = 0, r = n / 3;
	while (l != r) {
		int t = (l + r) / 2;
		if (r - l == 1)t = r;
		if (ok(t))l = t;
		else r = t - 1;
	}
	printf("%d\n", l);
}