#include #include #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; typedef long long LL; const int MAXN = 200010, MAXT = 1000010; const LL INF = 1LL << 60; int which[MAXT], type[MAXT], f[MAXT], t[MAXT]; LL A[MAXN], B[MAXN], C[MAXN]; bool reached[MAXN]; inline void addLine(int idx, LL a, LL b) { if (b < B[idx]) { A[idx] = a; B[idx] = b; } } inline LL minValue(int idx, LL x) { return A[idx] * x + B[idx]; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1, a, b, s, e; i <= m; i++) { scanf("%d%d%d%d", &a, &b, &s, &e); which[s] = which[e] = i; f[s] = a; t[e] = b; type[s] = +1; type[e] = -1; C[i] = INF; } for (int i = 1; i <= n; i++) { A[i] = 0; B[i] = INF; } addLine(1, 0, 0); reached[1] = true; LL ans = INF; for (int i = 0; i < MAXT; i++) { if (!which[i]) continue; if (type[i] == +1) { if (reached[f[i]]) { C[which[i]] = i * (LL) i + minValue(f[i], i); } } else { if (C[which[i]] < INF) { if (t[i] == n) ans = min(ans, C[which[i]]); addLine(t[i], -2 * i, C[which[i]] + i * (LL) i); reached[t[i]] = true; } } } printf("%lld\n", ans); return 0; }