#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> ord;
int f(int l, int r){
int ans = 0;
if(l + 1 >= r) return 0;
int mid = (l + r) / 2;
ans += f(l, mid) + f(mid, r);
int i = l, j = mid;
while(i < mid && j < r){
if(ord[i] < ord[j]){
i++;
}else{
ans += mid - i;
j++;
}
}
inplace_merge(ord.begin() + l, ord.begin() + mid, ord.begin() + r);
return ans;
}
signed main(){
cin >> n;
map<char, queue<int>> mp;
string s;
cin >> s;
string s2 = s;
reverse(s2.begin(), s2.end());
for(int i = 0; i < n; i++){
mp[s2[i]].push(i + 1);
}
for(int i = 0; i < n; i++){
ord.push_back(mp[s[i]].front());
mp[s[i]].pop();
}
cout << f(0, n) << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBuOwp2ZWN0b3I8aW50PiBvcmQ7IAoKaW50IGYoaW50IGwsIGludCByKXsKICAgIGludCBhbnMgPSAwOwoKCWlmKGwgKyAxID49IHIpIHJldHVybiAwOwoJaW50IG1pZCA9IChsICsgcikgLyAyOwoJYW5zICs9IGYobCwgbWlkKSArIGYobWlkLCByKTsKCWludCBpID0gbCwgaiA9IG1pZDsKCXdoaWxlKGkgPCBtaWQgJiYgaiA8IHIpewoJCWlmKG9yZFtpXSA8IG9yZFtqXSl7CgkJCWkrKzsKCQl9ZWxzZXsKCQkJYW5zICs9IG1pZCAtIGk7IAoJCQlqKys7CgkJfQoJfQoJaW5wbGFjZV9tZXJnZShvcmQuYmVnaW4oKSArIGwsIG9yZC5iZWdpbigpICsgbWlkLCBvcmQuYmVnaW4oKSArIHIpOwoJcmV0dXJuIGFuczsgCn0Kc2lnbmVkIG1haW4oKXsKCWNpbiA+PiBuOwoJbWFwPGNoYXIsIHF1ZXVlPGludD4+IG1wOwoJc3RyaW5nIHM7CgljaW4gPj4gczsKCXN0cmluZyBzMiA9IHM7CglyZXZlcnNlKHMyLmJlZ2luKCksIHMyLmVuZCgpKTsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewoJCW1wW3MyW2ldXS5wdXNoKGkgKyAxKTsKCX0KCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewoJCW9yZC5wdXNoX2JhY2sobXBbc1tpXV0uZnJvbnQoKSk7CgkJbXBbc1tpXV0ucG9wKCk7Cgl9Cgljb3V0IDw8IGYoMCwgbikgPDwgZW5kbDsgCgkKfQ==