#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define ff first
#define ss second
const int MAXM = 1e5 + 7;
ll dp[MAXM];
bool inter(int a, int b, int d, int a2, int b2, int d2) {
int r = abs(d - d2);
if (b > b2) {
swap(a, a2);
swap(b, b2);
}
b -= r;
return (b + 1) >= a2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, a, b, c, d;
cin >> n >> m;
vector<pair<pii, pii>> tab;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c >> d;
tab.push_back({{c, b}, {a, d}});
}
sort(tab.begin(), tab.end());
ll ans = 1e18;
for (int i = 0; i < m; i++) {
int l = tab[i].ff.ss;
int r = tab[i].ff.ff;
int t = tab[i].ss.ff;
int c = tab[i].ss.ss;
dp[i] = 1e18;
if (l == 1) {
dp[i] = c;
//cout << l << ' ' << r << ' ' << t << ' ' << c << ' ' << dp[i] << '\n';
continue;
}
for (int j = 0; j < i; j++) {
if (inter(l, r, t, tab[j].ff.ss, tab[j].ff.ff, tab[j].ss.ff)) {
dp[i] = min(dp[i], dp[j] + c);
// cout << "OH: " << l << ' ' << r << ' ' << t << ' ' << c << ": " << tab[j].ff.ss << ' ' << tab[j].ff.ff << ' ' << tab[j].ss.ff << ' ' << dp[j] + c << '\n';
}
}
// cout << l << ' ' << r << ' ' << t << ' ' << c << ' ' << dp[i] << '\n';
if (r == n) ans = min(ans, dp[i]);
}
if (ans == 1e18) ans = -1;
cout << ans << '\n';
return 0;
}