#include<bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    long long k;
    long long x,y;
    vector<pair<long long ,long long>> a;
    
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back({x,y});
    }
    
    sort(a.begin(), a.end());
    
    for(int i=0;i<n;i++){
        if(a[i].first > k) break;
        k += a[i].second;
    }
    cout<<k<<endl;
    return 0;
}
#include <bits/stdc++.h>

#define kyou using
#define mo namespace
#define kawaii std

kyou mo kawaii; // hi honey~

#define ll long long
#define ld long double
#define endl "\n"
const int MX = 200005;
const int LG = (int)log2(MX) + 1;
const int BLOCK = 205;
const int inf = 1000000069;
const ll inf_ll = 8000000000000000069ll;
const ll mod = 1e9 + 7;
const int dxh[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dyh[] = {2, -2, 2, -2, 1, -1, 1, -1}; // horse
const int dx[] = {0, 1, 0, -1, 0, 0};
const int dy[] = {1, 0, -1, 0, 0, 0}; // adj
const int dz[] = {0, 0, 0, 0, 1, -1}; // 3d
const int dxd[] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dyd[] = {1, 0, -1, -1, -1, 0, 1, 1}; // diag
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)

bool untied = 0;
void setIn(string s){freopen(s.c_str(), "r", stdin);}
void setOut(string s){freopen(s.c_str(), "w", stdout);}
void unsyncIO(){cin.tie(0) -> sync_with_stdio(0);}
void setIO(string s = ""){
	if(!untied) unsyncIO(), untied = 1;
	if(s.size()){
		setIn(s + ".in");
		setOut(s + ".out");
	}
}

int n, m;
vector<int> ps[2 * MX + 2];
vector<int> adj[2 * MX + 2];
int dist[2 * MX + 2];
bitset<1000005> vis;

map<pair<int, int>, int> id;
map<int, int> rev;

int main(){
	setIO();

	cin >> n >> m;
  vector<pair<int, int> > thing(m);
	for(int x, y, i = 0; i < m; i ++){
		cin >> thing[i].first >> thing[i].second;
	}
  vector<int> cc;
  for(int i = 0; i < m; i ++){
	cc.push_back(thing[i].second);
	if(thing[i].second > 0) cc.push_back(thing[i].second - 1);
	if(thing[i].second < 2 * n) cc.push_back(thing[i].second + 1);
  }
  cc.push_back(n);
  sort(cc.begin(), cc.end());
  cc.erase(unique(cc.begin(), cc.end()), cc.end());
  for(int i = 0; i < cc.size(); i ++)
    rev[cc[i]] = i;
  
	ps[rev[n]].push_back(0);
  	for(int x, y, i = 0; i < m; i ++){
		ps[rev[thing[i].second]].push_back(thing[i].first);
	}

	int cnt = 0;
	vector<int> ends;
	for(int i = 0; i < cc.size(); i ++){
		sort(ps[i].begin(), ps[i].end());
		for(int j = 0; j < ps[i].size(); j ++)
			id[{i, ps[i][j]}] = cnt ++;
		if(ps[i].size()) ends.push_back(cnt - 1);
	}

	for(int i = 0; i < cc.size(); i ++){
		for(int j = 0; j < ps[i].size(); j ++){
			// cout << cc[i] << " " << ps[i][j] << endl;
			if(cc[i] > 0 && i > 0 && ps[i - 1].size() && cc[i] == cc[i - 1] + 1){
				int x = lower_bound(ps[i - 1].begin(), ps[i - 1].end(), ps[i][j]) - ps[i - 1].begin();
				if(x != 0){
					x --;
					//assert(ps[i - 1][x] < ps[i][j] && (x + 1 == ps[i - 1].size() || ps[i - 1][x + 1] >= ps[i][j]));
					//cout << cc[i - 1] << " " << ps[i - 1][x] << " " << cc[i] << " " << ps[i][j] << endl;
					adj[id[{i - 1, ps[i - 1][x]}]].push_back(id[{i, ps[i][j]}]);
				}
			}
			if(cc[i] < 2 * n && i + 1 < cc.size() && ps[i + 1].size() && cc[i] == cc[i + 1] - 1){
				int x = lower_bound(ps[i + 1].begin(), ps[i + 1].end(), ps[i][j]) - ps[i + 1].begin();
				if(x != 0){
					x --;
					//assert(ps[i + 1][x] < ps[i][j] && (x + 1 == ps[i + 1].size() || ps[i + 1][x + 1] >= ps[i][j]));
					//cout << cc[i + 1] << " " << ps[i + 1][x] << " " << cc[i] << " " << ps[i][j] << endl;
					adj[id[{i + 1, ps[i + 1][x]}]].push_back(id[{i, ps[i][j]}]);
				}
			}
		}
	}

	queue<int> q;
	int st = id[{rev[n], 0}];
	q.push(st); vis = 0;

	for(; q.size();){
		int nw = q.front(); q.pop();
		if(vis[nw]) continue;
		vis[nw] = 1;

		for(int nx : adj[nw]) q.push(nx);
	}
	
	int ax = 0;
	for(int i : ends)
		if(vis[i]) ax ++;
	cout << ax << endl;
	return 0;
}