#include #include using namespace std; using i32 = int; using i64 = long long; using i128 = __int128_t; using p2 = pair; using el = tuple; using f64 = long double; using mint = atcoder::modint998244353; void _main(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(18); _main(); } i64 pow(i64 a, i64 n) { i64 res = 1; i64 x = a; while (n > 0) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } p2 op(p2 a, p2 b) {return min(a, b);} p2 e() {return {1e18, 1e18};} p2 mpn(i64 f, p2 x) {return x.second == -1 ? x : (p2){min(f, x.first), x.second};} i64 cmp(i64 f, i64 g) {return min(f, g);} i64 idn() {return 1e18;} p2 mps(i64 f, p2 x) {return {x.first + f, x.second};} i64 cms(i64 f, i64 g) {return f + g;} i64 ids() {return 0;} pair op1(pair a, pair b) {return a.first < b.first ? a : a.first > b.first ? b : (pair){a.first, a.second + b.second};} pair e1() {return {1e18, 0};} i64 opn(i64 a, i64 b) {return min(a, b);} i64 en() {return 1e18;} void _main() { i64 n, m; cin >> n >> m; vector> g(n); for (i64 i = 0; i < m; i++) { i64 x, l, r, c; cin >> x >> l >> r >> c; x--; l--; g[x].push_back({l, r, c}); } vector dist(n, 1e18); { atcoder::lazy_segtree seg(n); for (i64 i = 0; i < n; i++) seg.set(i, {1e18, i}); seg.set(0, {0, 0}); while (seg.all_prod().first != 1e18) { auto [d, i] = seg.all_prod(); seg.set(i, {1e18, -1}); dist[i] = d; for (auto [l, r, c] : g[i]) { seg.apply(l, r, d + c); } } } map> mp; for (i64 i = 0; i < n; i++) { mp[dist[i]].push_back(i); } vector ord; for (auto [x, v] : mp) { atcoder::lazy_segtree seg(v.size()); for (i64 i = 0; i < v.size(); i++) { seg.set(i, {0, i}); } for (i64 i = 0; i < v.size(); i++) { for (auto [l, r, c] : g[v[i]]) { if (c != 0) continue; i64 i1 = lower_bound(v.begin(), v.end(), l) - v.begin(); i64 i2 = lower_bound(v.begin(), v.end(), r) - v.begin(); seg.apply(i1, i2, 1); } } while (seg.all_prod().first == 0) { auto [d, i] = seg.all_prod(); seg.set(i, {1e18, 1e18}); ord.push_back(v[i]); for (auto [l, r, c] : g[v[i]]) { if (c != 0) continue; i64 i1 = lower_bound(v.begin(), v.end(), l) - v.begin(); i64 i2 = lower_bound(v.begin(), v.end(), r) - v.begin(); seg.apply(i1, i2, -1); } } for (i64 i = 0; i < v.size(); i++) { if (seg.get(i).first != 1e18) { cout << "Too Many\n"; return; } } } i128 ans = 1; atcoder::lazy_segtree, op1, e1, pair, op1, op1, e1> seg(n); atcoder::fenwick_tree bit(n); seg.set(0, {0, 1}); for (i64 i = 0; i < n; i++) bit.add(i, 1); for (i64 i : ord) { auto [d, cnt] = seg.get(i); for (auto [l, r, c] : g[i]) { seg.apply(l, r, {d + c, cnt}); ans += bit.sum(l, r) * cnt; if (ans > (i128)1e18) { cout << "Too Many\n"; return; } } bit.add(i, -1); } cout << (i64)ans << "\n"; }