
#include <cstdio>
#include <cstring>

int n;
char rawstr[1234567];
char str[1234567];

bool cond(int x)
{
	int lastco = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		if (str[i] == 'I')
		{
			str[i] = 'X';
			lastco++;
			if (lastco == x)break;
		}
	}
	if (lastco != x)return false;
	int xco = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		if (str[i] == 'X')
		{
			++xco;
		}
		if (str[i] == 'O' && xco > 0)
		{
			--xco;
			str[i] = 'Y';
		}
	}
	if (xco != 0)return false;
	int yco = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		if (str[i] == 'Y')
		{
			++yco;
		}
		else if (str[i] != 'X' && str[i] != 'O' && yco > 0)
		{
			--yco;
			str[i] = 'Z';
		}
	}
	if (yco != 0)return false;
	return true;
}

int main()
{
	scanf("%d", &n);
	scanf("%s", rawstr);
	int lower = 0;
	int upper = n;
	while (lower + 1 < upper)
	{
		strcpy(str, rawstr);
		int middle = (lower + upper) / 2;
		if (cond(middle))
		{
			lower = middle;
		}
		else
		{
			upper = middle;
		}
	}
	printf("%d\n", lower);
	return 0;
}