#include #include #include #include using namespace std ; typedef long long ll ; const ll INFTY = 1LL << 60 ; vector ta, tb ; vector ca, cb ; vector > perc ; vector > xint ; vector minf ; void error(const char *s) { cerr << s << endl ; exit(0) ; } ll looping = 0 ; int main() { int n, m ; cin >> n >> m ; ta.resize(m) ; tb.resize(m) ; ca.resize(m) ; cb.resize(m) ; vector > sa ; minf.resize(m) ; for (int i=0; i> ca[i] >> cb[i] >> ta[i] >> tb[i] ; sa.push_back(make_pair(tb[i], i)) ; if (ca[i] == 1) minf[i] = min(minf[i], ta[i]*ta[i]) ; } sort(sa.begin(), sa.end()) ; perc.resize(n+1) ; xint.resize(n+1) ; ll r = INFTY ; for (int i=0; i 0) starti-- ; // sloppy coding for (int pfi=starti; pfi ta[fi]) break ; } minf[fi] = mf ; if (mf == INFTY) continue ; int cit = cb[fi] ; if (cit == n) r = min(r, mf) ; ll x = -1 ; while (1) { if (perc[cit].size() == 0) break ; int pi = perc[cit][perc[cit].size()-1] ; ll ai = minf[pi] ; ll ei = tb[pi] ; ll aj = mf ; ll ej = tb[fi] ; if (ei == ej) { if (ai <= aj) { x = -INFTY ; } else { x = INFTY ; } } else { x = ((ai+ei*ei)-(aj+ej*ej)) / (2 * (ei-ej)) ; } if (x < tb[fi]) x = tb[fi] ; if (xint[cit].size() > 0 && x < xint[cit][xint[cit].size()-1]) { perc[cit].pop_back() ; xint[cit].pop_back() ; x = -1 ; } else { break ; } } if (fi >= 0) { perc[cit].push_back(fi) ; if (x != -1) xint[cit].push_back(x) ; } } // cerr << "Looping is " << looping << endl ; if (r >= INFTY) cout << "No route." << endl ; else cout << r << endl ; }