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

using namespace std;

using Pair = pair<int, int>;
using lint = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)

// const int mod = 1e9 + 7;
// const int mod = 998244353;

template <class T>
inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

inline void Yes(bool condition) {
    if (condition) {
        std::cout << "Yes" << std::endl;
    } else {
        std::cout << "No" << std::endl;
    }
}

int main() {
    int n;
    string s;
    cin>>n>>s;
    vector<vector<int>>  cnt(n+1, vector<int>(4, 0));
    rep(i,n){
        if (s[i] == 'A') cnt[i+1][0]++;
        else if(s[i]=='T')
            cnt[i+1][1]++;
        else if(s[i]=='C')
            cnt[i+1][2]++;
        else
            cnt[i+1][3]++;
    }
    rep(i,n){
        rep(j,4){
            cnt[i + 1][j] += cnt[i][j];
        }
    }
    int ret = 0;
    for (int i = 1; i <= n;i++) {
        for (int j = i; j <= n; j++) {
            if(cnt[j][0]-cnt[i-1][0]==cnt[j][1]-cnt[i-1][1]&&
               cnt[j][2]-cnt[i-1][2]==cnt[j][3]-cnt[i-1][3]){
                ret++;
                //cout << i << " " << j << endl;
            }
        }
    }
    cout << ret << endl;
    return 0;
}

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    vector<ll> H(N), W(M);
    rep(i, N) { cin >> H[i]; }
    rep(i, M) { cin >> W[i]; }

    sort(all(H));
    sort(all(W));
    vector<ll> suml(N), sumr(N);
    rep(i, N - 1) {
        suml[i] = abs(H[i + 1] - H[i]);
        i++;
    }

    rep(i, N - 1) { suml[i + 1] += suml[i]; }

    for (int i = N - 1; i > 0; i--) {
        sumr[i] = abs(H[i] - H[i - 1]);
        i--;
    }

    for (int i = N - 1; i > 0; i--) {
        sumr[i - 1] += sumr[i];
    }

    ll ans = INF;
    rep(i, M) {
        ll itr = lower_bound(all(H), W[i]) - H.begin(), sum = 0;
        if (itr % 2 == 0) {
            sum += abs(H[itr] - W[i]);
            int l = itr - 1, r = itr + 1;
            if (l >= 0) {
                sum += suml[l];
            }
            if (r < N) {
                sum += sumr[r];
            }
        } else {
            sum += abs(H[itr - 1] - W[i]);
            int l = itr - 2, r = itr + 1;
            if (l >= 0) {
                sum += suml[l];
            }
            if (r < N) {
                sum += sumr[r];
            }
        }

        ans = min(ans, sum);
    }

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