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;
namespace mine {
long long qread() {
  long long ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) putchar('-'), num = -num;
  if (num >= 10) write(num / 10);
  putchar('0' + num % 10);
}
void write1(long long num) {
  write(num);
  putchar(' ');
}
void write2(long long num) {
  write(num);
  putchar('\n');
}
template <typename T>
void chmax(T &x, const T y) {
  x = (x > y ? x : y);
}
template <typename T>
void chmin(T &x, const T y) {
  x = (x < y ? x : y);
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int N = 2e6 + 10;
int n, m;
vector<pair<int, pair<int, int> > > to[N];
vector<pair<int, int> > hh[N], real[N][2];
int findid(int op, int x, int tim) {
  return upper_bound((real[x][op]).begin(), (real[x][op]).end(),
                     make_pair(tim, INF)) -
         real[x][op].begin() - 1;
}
int dis[N];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    qq;
struct Edge {
  int x, y, l, r;
} e[N];
int cur[N][2];
pair<int, int> bak[N];
pair<int, int> fix(pair<int, int> now, int op) {
  if ((now.first & 1) ^ op) now.first++;
  return now;
}
void main() {
  n = qread(), m = qread();
  for (int i = (1), I = (m); i <= I; i++) {
    int x = qread(), y = qread(), l = qread(), r = qread();
    hh[x].push_back({l, r});
    hh[y].push_back({l, r});
    e[i] = {x, y, l, r};
  }
  int tim = 1;
  for (int x = (1), I = (n); x <= I; x++)
    if (((int)(hh[x]).size())) {
      sort((hh[x]).begin(), (hh[x]).end());
      for (int op = (0), I = (1); op <= I; op++) {
        pair<int, int> now = fix(hh[x][0], op);
        for (int t = (1), I = (((int)(hh[x]).size()) - 1); t <= I; t++) {
          pair<int, int> h = fix(hh[x][t], op);
          if (h.first >= h.second) continue;
          if (now.first >= now.second) {
            now = h;
            continue;
          }
          if (h.first <= now.second)
            chmax(now.second, h.second);
          else
            real[x][op].push_back(now), now = h;
        }
        if (now.first < now.second) real[x][op].push_back(now);
        cur[x][op] = tim, tim += ((int)(real[x][op]).size());
        for (int i = (cur[x][op]), I = (tim - 1); i <= I; i++) bak[i] = {x, op};
      }
    }
  if (n == 1) {
    write(0);
    return;
  }
  if (!((int)(real[1][0]).size()) or real[1][0][0].first > 0) {
    write(-1);
    return;
  }
  for (int i = (1), I = (m); i <= I; i++) {
    int x = e[i].x, y = e[i].y;
    for (int op = (0), I = (1); op <= I; op++) {
      pair<int, int> now = fix({e[i].l, e[i].r}, op);
      if (now.first >= now.second) continue;
      to[cur[x][op] + findid(op, x, now.first)].push_back({y, now}),
          to[cur[y][op] + findid(op, y, now.first)].push_back({x, now});
    }
  }
  memset(dis, 0x3f, sizeof dis);
  dis[1] = 0;
  qq.push({0, 1});
  int ans = INF;
  while (((int)(qq).size())) {
    pair<int, int> now = qq.top();
    qq.pop();
    int rx = now.second, x = bak[rx].first, op = bak[rx].second;
    if (now.first != dis[rx]) continue;
    for (auto t : to[rx]) {
      int y = t.first, l = t.second.first, r = t.second.second,
          go = max(now.first, l);
      if (go >= r) continue;
      if (y == n) {
        chmin(ans, go + 1);
        continue;
      }
      int ry = cur[y][op ^ 1] + findid(op ^ 1, y, go + 1);
      if (findid(op ^ 1, y, go + 1) >= 0 and dis[ry] > go + 1)
        dis[ry] = go + 1, qq.push({go + 1, ry});
    }
  }
  write(ans == INF ? -1 : ans);
}
};  // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}
