This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> t(m), l(m), r(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> t[i] >> l[i] >> r[i] >> c[i];
    --l[i];
  }
  vector<vector<int>> g(m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (i == j) {
        continue;
      }
      if (t[i] >= t[j]) {
        if (l[j] + (t[i] - t[j]) <= r[i]) {
          g[i].push_back(j);
        }
      } else {
        if (r[i] - (t[j] - t[i]) >= l[j]) {
          g[i].push_back(j);
        }
      }
    }
  }
  const long long inf = (long long) 1e18;
  vector<long long> dist(m, inf);
  set<pair<long long, int>> st;
  for (int i = 0; i < m; i++) {
    if (l[i] == 0) {
      dist[i] = c[i];
      st.emplace(dist[i], i);
    }
  }
  while (!st.empty()) {
    auto it = st.begin();
    int i = it->second;
    st.erase(it);
    for (int j : g[i]) {
      if (dist[j] > dist[i] + c[j]) {
        if (dist[j] != inf) {
          st.erase(st.find({dist[j], j}));
        }
        dist[j] = dist[i] + c[j];
        st.emplace(dist[j], j);
      }
    }
  }
  long long res = inf;
  for (int i = 0; i < m; i++) {
    if (r[i] == n) {
      res = min(res, dist[i]);
    }
  }
  cout << (res == inf ? -1 : res) << '\n';
  return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |