#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
string S;
vector<P> OI;
bool check(int h){
  //cout << "h " << h << endl;
  int tk=S.size()-1;
  if( OI.size() < h ) return false;
  for(int i=0;i<h;i++){
    tk=min( tk, OI[i].second );
  }
  int res = 0;
  int H=h;
  h--;
  for(int i=0;i<(int)S.size()&&h>-1;i++){
    if( (i<tk&&S[i] == 'I') || S[i] == 'J' ){
      //cout << i << " -> ";
      while( h>-1 && i >= OI[h].first ) h--;
      if( h>-1 ){
        //cout << OI[h].first << " -> " << OI[h].second << endl;
        res++;
        h--;
      } 
    }
  }
  // cout << H << " "<< res << endl;
  return res==H;
}
int N;
int main(){
  cin >> N;
  cin >> S;
  int st=0,ed=(int)S.size()/3;
  int oc=S.size()-1;
  for(int i=(int)S.size()-1;i>-1;i--){
    oc = min(oc,i-1);
    if( S[i] == 'I' ){
      P p = P(-1,i);
      for(;oc>-1;oc--){
        if( S[oc] == 'O' ){
          p.first=oc;
          oc--;
          break;
        }
      }
      if( p.first!=-1 ) OI.push_back( p );
    }
  }
  //for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second << endl;
  
  int res = 0;  
  while(st<=ed){
    int h=(st+ed)/2;
    if( check(h) ){
      st = h+1;
      res=h;
    } else {
      ed = h-1;
    }
  }
  cout << res << endl;
}