#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
//<< fixed << setprecision(n) 小数第n位まで表示
template <class T> void chmin(T& a, T b) {
    if(a > b) {
        a = b;
    }
}
template <class T> void chmax(T& a, T b) {
    if(a < b) {
        a = b;
    }
}

int main() {
    int N, K;
    cin >> N >> K;
    vector<pair<ll,int>> data(N);
    rep(i, N) {
      cin >> data[i].first >> data[i].second;
    }
    sort(data.begin(),data.end());
    //rep(i, N) cout << data[i].second << endl;
    ll ans = K;
    rep(i, N) {
      if(data[i].first <= ans) {
        ans += data[i].second;
        //cout << "ok" << endl;
      }
      //cout << data[i].first << endl;
    }
    cout << ans << endl;
    return 0;
}
/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
    Mbak Seulgi Dong
*/
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    int n,i,j,k,x,y,ans,temp,curr,m;
    int tc;
    string s;
    cin >> n >> s;
    s = " " + s;
    ll seulgi = 0;
    map<pair<int,int>, int> mp;
    mp[{0, 0}] = 1;
    x = 0, y = 0;
    for(i = 1; i <= n; i++) {
        char c = s[i];
        if(c == 'A') x++;
        if(c == 'T') x--;
        if(c == 'G') y++;
        if(c == 'C') y--;
        seulgi += mp[{x, y}];
        mp[{x, y}]++;
    }
    cout << seulgi << endl;
    return 0;
}
