#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define pb push_back

map<string, bool> visited;
vector<vector<string>> adj;

/*void dfs(string s, ll &k)
{
    visited[s] = 1;
    ll len = adj[a].size();

    for (int i = 0; i < len; i++)
    {
        if (!visited[adj[a][i]])
        {
            k++;
            dfs(adj[a][i], k);
        }
    }
}*/

int main()
{
    ll n, k;
    cin >> n >> k;
    map<ll, ll> m;
    vector<pair<ll, ll>> v;
    priority_queue<ll, vector<ll>, std::greater<ll>> q;
    ll x, y;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        if (m[x] != 0)
            m[x] += y;
        else
        {
            m[x] = y;
            q.push(x);
        }
    }
    ll pos = 0;
    k += m[0];
    while (k > 0){
        pos += k;
        k=0;
        while(!q.empty()&&q.top() <= pos){
            k += m[q.top()];
            q.pop();
        }

    }
    cout << pos << endl;
}
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1002003004005006007ll;

  vector<int> G[2000];
void solve(){
  int n,m;cin >> n >> m;
  int a,b;
  rep(i,m){
    cin >> a >> b;
    a--;b--;
    G[a].push_back(b);
  }
  ll cnt = 0;
  queue<int> Q;
  rep(i,n){
    Q.push(i);
    vector<bool> memo(n,0);
    memo[i] = 1;
    while(!Q.empty()){
      auto p = Q.front();Q.pop();cnt++;
      rep(j,G[p].size()){
        if(memo[G[p][j]]==0){
          memo[G[p][j]] = 1;
          Q.push(G[p][j]);
        }
      }
    }
  }
  cout << cnt << endl;
  return;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(16);

  solve();
    return 0;
}