#include <bits/stdc++.h>
using namespace std;
#define REP(i,N) for(i=0;i<N;i++)
typedef long long ll;
typedef pair<int, int> P;
typedef struct{
  int first;
  int second;
  int third;
}T;

//昇順
bool comp_Se(T& l, T& r){
  return l.second < r.second;
}

int main(void){
  int i,j,N,num=0;
  string S;
  cin >> N >> S;
  REP(i,N-2){
    if(S[i] != 'U') continue;
    for(j=i+1;2*j-i<N;j++){
      if(S[j] == 'M' && S[2*j-i] == 'G') num++;
    }
  }
  cout << num << endl;
  return 0;
}