#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


int main(){
    int n;
    string s;
    cin >> n >> s;
    ll ans = 0;
    rep(i,n){
        if(s[i]=='M'){
            rep(j,i){
                if(s[j]=='U'){
                    if(2*i-j<n&&s[2*i-j]=='G'){
                        ans++;
                    }
                }
            }
        }
    }

    cout << ans << endl;
    return 0;
}