#include <bits/stdc++.h>using namespace std;
#define int long longconst int N = 10005;
const int M = 100005;
struct Edge { int to, w; };
vector<Edge> g[N * 3];
struct Node {
int id, dis;
bool operator<(const Node& x) const { return dis > x.dis; }
};
int dis[N * 3];
void add(int u, int v, int w) {
g[u].push_back({v, w});
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (d == 0) {
for (int k = 0; k < 3; k++) {
add(a + k * n, b + k * n, c);
add(b + k * n, a + k * n, c);
}
} else {
for (int k = 0; k < 2; k++) {
add(a + k * n, b + (k + 1) * n, c);
add(b + k * n, a + (k + 1) * n, c);
}
}
}
int S = 1;
memset(dis, 0x3f, sizeof dis);
dis[S] = 0;
priority_queue<Node> q;
q.push({S, 0});
while (!q.empty()) {
auto [x, d] = q.top(); q.pop();
if (d > dis[x]) continue;
for (auto& e : g[x]) {
int y = e.to, w = e.w;
if (dis[y] > dis[x] + w) {
dis[y] = dis[x] + w;
q.push({y, dis[y]});
}
}
}
int original = dis[n];
int after = min({dis[n], dis[n + n], dis[n + 2 * n]});
cout << original - after << '\n';
return 0;
}