#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pii;

struct Edge{
    int to;
    ll weight;
    ll bias;
    Edge(int to, ll weight, ll bias):to(to), weight(weight), bias(bias){}
};
using Graph = vector<vector<Edge>>;

const ll INF = 1LL<<60;

vector<ll> Dijkstra(Graph &G, int start){
    int graph_size = (int)G.size();
    vector<ll> distance(graph_size, INF);
    priority_queue<pair<ll, int>,
                    vector<pair<ll, int>>,
                    greater<pair<ll, int>>> que;
    distance[start] = 0;
    que.push(make_pair(distance[start], start));
    while(!que.empty()){
        auto [dist ,visit] = que.top();
        que.pop();
        if (distance[visit] != dist) continue;
        for(Edge& next_edge : G[visit]){
            ll x = sqrt(next_edge.bias) - dist;
            ll next_dist = dist + next_edge.weight;
            if(x > 0)
                next_dist += min(next_edge.bias / (dist+1),
                                    min(x + (next_edge.bias / (dist + x + 1)),
                                        x + 1 + (next_edge.bias / (dist + x ))
                                        )
                                );
            else
                next_dist += next_edge.bias / (dist+1);
            if(distance[next_edge.to] <= next_dist) continue;
            distance[next_edge.to] = next_dist;
            que.push(make_pair(distance[next_edge.to], next_edge.to));
        }
    }
    return distance;
}

int main(){
    int n, m;    cin >> n >> m;
    Graph G(n+1);
    rep(i, m){
        ll a,b,c,d; cin >> a >> b >> c >> d;
        if(a==b)    continue;
        G[a].push_back(Edge(b, c, d));
        G[b].push_back(Edge(a, c, d));
    }
    auto dis = Dijkstra(G, 1);
    if(dis[n] == 1LL<<60)  dis[n] = -1;
    cout << dis[n] << endl;
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define endl "\n"
using ll=long long;
using ld=long double;
using namespace std;
ll mod= 1e9+7;
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }

vector<pll> z;
int main(){
  ll n,k;
  cin>>n>>k;
  rep(i,0,n){
    ll x,y;
    cin>>x>>y;
    z.pb(mp(x,y));
  }
  sort(z.begin(),z.end());
  ll pos=0,ans=0;
  ll pt =k;
  rep(i,0,z.size()){
    if(pt-z[i].first+pos>=0){
      pt-=z[i].first-pos;
      pt+=z[i].second;
      pos=z[i].first;
    }
    else{
      break;
    }
  }
  ans=pos+pt;
  cout<<ans;
}