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;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long nmax = 500005;
long long n, m, oddpt[nmax], evenpt[nmax];
struct edge {
  long long to, l, r;
  friend bool operator<(edge A, edge B) { return A.l < B.l; }
};
vector<edge> nod[nmax];
struct event {
  long long start, ending, to, par;
  friend bool operator<(event A, event B) { return A.start > B.start; }
};
priority_queue<event> pq;
int32_t main() {
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long a, b, l, r;
    cin >> a >> b >> l >> r;
    nod[a].push_back({b, l, r});
    nod[b].push_back({a, l, r});
  }
  for (long long i = 1; i <= n; i++) {
    sort((nod[i]).begin(), (nod[i]).end());
  }
  pq.push({0, 0, 1, 0});
  while (pq.size()) {
    auto it = pq.top();
    pq.pop();
    if (it.to == n) {
      return cout << it.start, 0;
    } else {
      if (it.par == 0) {
        for (long long i = evenpt[it.to]; i < (long long)nod[it.to].size();
             i++) {
          if (nod[it.to][i].l > it.ending) {
            break;
          }
          if (it.ending >= nod[it.to][i].l && it.start < nod[it.to][i].r) {
            long long start = max(it.start + 1, nod[it.to][i].l + 1);
            if (start % 2 == 0) start++;
            long long ending = nod[it.to][i].r;
            if (ending % 2 == 0) ending--;
            if (start <= ending) {
              pq.push({start, ending, nod[it.to][i].to, 1 - it.par});
            }
          }
          evenpt[it.to]++;
        }
      } else {
        for (long long i = oddpt[it.to]; i < (long long)nod[it.to].size();
             i++) {
          if (nod[it.to][i].l > it.ending) {
            break;
          }
          if (it.ending >= nod[it.to][i].l && it.start < nod[it.to][i].r) {
            long long start = max(it.start + 1, nod[it.to][i].l + 1);
            if (start % 2 == 1) start++;
            long long ending = nod[it.to][i].r;
            if (ending % 2 == 1) ending--;
            if (start <= ending) {
              pq.push({start, ending, nod[it.to][i].to, 1 - it.par});
            }
          }
          oddpt[it.to]++;
        }
      }
    }
  }
  return cout << -1, 0;
}
