#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 len[MAXN], base[MAXN], which[MAXT], type[MAXT], f[MAXT], t[MAXT]; LL A[MAXN], B[MAXN], C[MAXN]; inline void addLine(int idx, LL a, LL b) { if (idx == 0) return; int l; while ((l = len[idx]) >= 2 + base[idx-1] && (B[l-2] - B[l-1]) * (a - A[l-1]) >= (B[l-1] - b) * (A[l-1] - A[l-2])) { --len[idx]; } A[l] = a; B[l] = b; len[idx]++; } inline LL minValue(int idx, LL x) { if (idx == 0) return INF; LL ret = INF; for (int i = base[idx-1]; i < len[idx]; ++i) { ret = min(ret, A[i] * x + B[i]); } return ret; } int main() { int n, m; scanf("%d%d", &n, &m); base[1]++; 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; base[b]++; C[i] = INF; } for (int i = 1; i <= n; i++) { base[i] += (len[i] = base[i-1]); } addLine(1, 0, 0); LL ans = INF; for (int i = 0; i < MAXT; i++) { if (!which[i]) continue; if (t[i] == n) ans = min(ans, C[which[i]]); if (C[which[i]] < ans) addLine(t[i], -2 * i, C[which[i]] + i * (LL) i); C[which[i]] = i * (LL) i + minValue(f[i], i); } printf("%lld\n", ans); return 0; }