#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <map> //http://vivi.dyndns.org/tech/cpp/map.html
#include <set> //http://vivi.dyndns.org/tech/cpp/set.html
#include <vector>
#include <deque>
#include <queue>
#include <numeric> //gcd,lcm c++17
#include <tuple>
#include <iomanip> //setprecision
#include <unordered_map>
#include <chrono>
//#define _GLIBCXX_DEBUG
using namespace std;
template <typename T>
using vc = vector<T>;
template <typename T>
using vv = vc<vc<T>>;
template <typename T>
using PQ = priority_queue<T, vc<T>, greater<T>>;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
//typedef __uint128_t Int;
using vi = vc<int>;
using vl = vc<ll>;
using vd = vc<double>;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
#define sz(x) (int)(x).size()
template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second
{
    //sort_pair_second<ll,ll,greater<ll>>()
    bool operator()(const std::pair<T1, T2> &left, const std::pair<T1, T2> &right)
    {
        Pred p;
        return p(left.second, right.second);
    }
};
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
int MOD = 1e9 + 7;
double EPS = 1e-9;
int INF = 2000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
int rdx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int rdy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int N;
string S;

int f(int n)
{
    if (n == 0)
        return 0;
    if (n == 1)
        return 1;
    return n + f(n - 1);
}

int main()
{
    cin >> N >> S;
    int left = 0, right = 0;
    int n = N - N % 2;
    int nextright = n;
    int nextleft = 0;
    int a = 0, g = 0, c = 0, t = 0;
    ll ans = 0;
    while (nextright > 0)
    {
        while (right < nextright)
        {
            if (S[right] == 'A')
                a++;
            else if (S[right] == 'G')
                g++;
            else if (S[right] == 'C')
                c++;
            else if (S[right] == 'T')
                t++;
            right++;
        }
        while (left < nextleft)
        {
            if (S[left] == 'A')
                a--;
            else if (S[left] == 'G')
                g--;
            else if (S[left] == 'C')
                c--;
            else if (S[left] == 'T')
                t--;
            left++;
        }
        if (a == t && g == c)
            ans++;
        nextright = right + 1;
        nextleft = left + 1;
        if (right == N)
        {
            n -= 2;
            right = 0;
            left = 0;
            nextright = n;
            nextleft = 0;
            a = 0, g = 0, c = 0, t = 0;
            continue;
        }
    }
    cout << ans << endl;
    /*
    vi a, g;
    int acnt = 0, gcnt = 0;
    int ans = 0;
    FOR(i, 0, N - 1)
    {
        if ((S[i] == 'A' && S[i + 1] == 'T') || (S[i] == 'T' && S[i + 1] == 'A'))
        {
            acnt++;
            ans++;
        }
        else if (acnt)
        {
            acnt--;
            a.push_back(acnt / 2);
            acnt = 0;
        }
        if ((S[i] == 'G' && S[i + 1] == 'C') || (S[i] == 'C' && S[i + 1] == 'G'))
        {
            ans++;
            gcnt++;
        }
        else if (gcnt)
        {
            gcnt--;
            g.push_back(gcnt / 2);
            gcnt = 0;
        }
    }
    if (acnt)
    {
        acnt--;
        a.push_back(acnt / 2);
    }
    if (gcnt)
    {
        gcnt--;
        g.push_back(gcnt / 2);
        gcnt = 0;
    }
    for (auto x : a)
        ans += f(x);
    for (auto x : g)
        ans += f(x);*/
}
//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define gcd(a,b) __gcd(a,b)
#define endl '\n'
const int N=3e5+10;
const int inf=1e9;
const int mod=1e9+7;

int  main(int argc, char const *argv[])
{
	/*#ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
    #endif*/
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);

	int n, k;
	cin >> n >> k;

	int a[n];
	int cnt[N]={0};

	for(int i=0; i<n; i++)
	{
		cin >> a[i];
		cnt[a[i]]++;
	}
	
	int ans=0;

	for(int i=0; i<N; i++)
	{
		if(cnt[i]<k)
		{
			int tmp=k-cnt[i];
			if(tmp>k)
				tmp=k;
			ans+=tmp*i;
			k-=tmp;
		}

		if(k==0)
			break;
	}

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