Arkady likes to walk around his kitchen. His labyrinthine kitchen consists of several important places connected with passages. Unfortunately it happens that these passages are flooded with milk so that it's impossible to pass through them. Namely, it's possible to pass through each passage in any direction only during some time interval.

The lengths of all passages are equal and Arkady makes through them in one second. For security reasons, Arkady can never stop, also, he can't change direction while going through a passage. In other words, if he starts walking in some passage, he should reach its end and immediately leave the end.

Today Arkady needs to quickly reach important place n from place 1. He plans to exit the place 1 at time moment 0 and reach the place n as early as he can. Please find the minimum time he should spend on his way.

Input

The first line contains two integers n and m (1 ≤ n ≤ 5·105, 0 ≤ m ≤ 5·105) — the number of important places and the number of passages, respectively.

After that, m lines follow, each of them describe one passage. Each line contains four integers a, b, l and r (1 ≤ a, b ≤ n, a ≠ b, 0 ≤ l < r ≤ 109) — the places the passage connects and the time segment during which it's possible to use this passage.

Output

Print one integer — minimum time Arkady should spend to reach the destination. If he can't reach the place n, print -1.

Examples

Input

5 6
1 2 0 1
2 5 2 3
2 5 0 1
1 3 0 1
3 4 1 2
4 5 2 3


Output

3


Input

2 1
1 2 1 100


Output

-1

Note

In the first example Arkady should go through important places 1 → 3 → 4 → 5.

In the second example Arkady can't start his walk because at time moment 0 it's impossible to use the only passage.
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
struct E {
  int v, l, r;
  E(int v = 0, int l = 0, int r = 0) : v(v), l(l), r(r) {}
  bool operator<(const E &e) const {
    if (l != e.l) return l < e.l;
    return r < e.r;
  }
};
vector<E> adj[1000100];
int cur[1000100];
priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >,
               greater<pair<pair<int, int>, int> > >
    q;
void zou(int u, int tl, int tr) {
  if (cur[u] == adj[u].size()) return;
  E &e = adj[u][cur[u]];
  if (e.l > tr) return;
  cur[u]++;
  if (tl <= e.r)
    q.push(pair<pair<int, int>, int>(pair<int, int>(max(tl, e.l) + 1, e.r + 1),
                                     e.v));
  zou(u, tl, tr);
}
int main() {
  int n, m;
  gn(n, m);
  for (int i = 0; i < m; i++) {
    int u, v, l, r;
    gn(u, v);
    gn(l, r);
    r--;
    if (!(l == r && l % 2 == 1)) {
      adj[u].push_back(E(n + v, l + (l & 1), r - (r & 1)));
      adj[v].push_back(E(n + u, l + (l & 1), r - (r & 1)));
    }
    if (!(l == r && l % 2 == 0)) {
      adj[n + u].push_back(E(v, l + (~l & 1), r - (~r & 1)));
      adj[n + v].push_back(E(u, l + (~l & 1), r - (~r & 1)));
    }
  }
  for (int i = 1; i <= n << 1; i++) sort(adj[i].begin(), adj[i].end());
  int ans = -1;
  q.push(pair<pair<int, int>, int>(pair<int, int>(0, 0), 1));
  while (!q.empty()) {
    pair<pair<int, int>, int> now = q.top();
    q.pop();
    if ((now.second == n || now.second == n << 1) &&
        (ans == -1 || ans > now.first.first))
      ans = now.first.first;
    zou(now.second, now.first.first, now.first.second);
  }
  printf("%d\n", ans);
  return 0;
}
