#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') t = -1;
    c = getchar();
  }
  while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
  return s * t;
}
const int N = 5e5 + 5, inf = 1e9 + 7;
inline void Min(int &x, int v) {
  if (x > v) x = v;
}
struct edge {
  int to, l, r, id;
  bool operator<(const edge &rhs) const { return l > rhs.l; }
} A[N << 2];
int cnt = 0;
vector<edge> S[2][N];
int dis[N << 2];
struct node {
  int x, d;
  node(int a = 0, int b = 0) { x = a, d = b; }
  bool operator<(const node &rhs) const { return d > rhs.d; }
};
priority_queue<node> q;
inline void add0(int a, int b, int c, int d) {
  if (c & 1) c++;
  if (d & 1) d--;
  if (c > d) return;
  A[++cnt].to = b, A[cnt].l = c, A[cnt].r = d, A[cnt].id = cnt;
  S[0][a].push_back(A[cnt]);
  if (a == 1 && !c)
    dis[cnt] = c, q.push(node(cnt, dis[cnt]));
  else
    dis[cnt] = inf;
}
inline void add1(int a, int b, int c, int d) {
  if (!(c & 1)) c++;
  if (!(d & 1)) d--;
  if (c > d) return;
  A[++cnt].to = b, A[cnt].l = c, A[cnt].r = d, A[cnt].id = cnt;
  S[1][a].push_back(A[cnt]);
  if (a == 1 && !c)
    dis[cnt] = c, q.push(node(cnt, dis[cnt]));
  else
    dis[cnt] = inf;
}
int main() {
  int n = read(), m = read();
  if (n == 1) return puts("0"), 0;
  for (register int i = 1; i <= m; ++i) {
    int a = read(), b = read(), c = read(), d = read() - 1;
    add0(a, b, c, d);
    add1(a, b, c, d);
    add0(b, a, c, d);
    add1(b, a, c, d);
  }
  for (register int i = 1; i <= n; ++i) {
    sort(S[0][i].begin(), S[0][i].end()), sort(S[1][i].begin(), S[1][i].end());
  }
  int x, xd, y;
  vector<edge>::iterator it;
  while (!q.empty()) {
    x = q.top().x, xd = q.top().d, q.pop();
    if (xd != dis[x]) continue;
    if (A[x].l & 1) {
      y = A[x].to, xd++;
      while (S[0][y].size()) {
        it = --S[0][y].end();
        if (it->l > A[x].r + 1) break;
        if (it->r < xd) {
          S[0][y].pop_back();
          continue;
        }
        if (dis[it->id] > max(it->l, xd)) {
          q.push(node(it->id, dis[it->id] = max(it->l, xd)));
        }
        S[0][y].pop_back();
      }
    } else {
      y = A[x].to, xd++;
      while (S[1][y].size()) {
        it = --S[1][y].end();
        if (it->l > A[x].r + 1) break;
        if (it->r < xd) {
          S[1][y].pop_back();
          continue;
        }
        if (dis[it->id] > max(it->l, xd)) {
          q.push(node(it->id, dis[it->id] = max(it->l, xd)));
        }
        S[1][y].pop_back();
      }
    }
  }
  int ans = inf;
  for (register int i = 1; i <= cnt; ++i)
    if (A[i].to == n) Min(ans, dis[i]);
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans + 1 << endl;
  return 0;
}

### Prompt

Create a solution in Cpp for the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') t = -1;
    c = getchar();
  }
  while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
  return s * t;
}
const int N = 5e5 + 5, inf = 1e9 + 7;
inline void Min(int &x, int v) {
  if (x > v) x = v;
}
struct edge {
  int to, l, r, id;
  bool operator<(const edge &rhs) const { return l > rhs.l; }
} A[N << 2];
int cnt = 0;
vector<edge> S[2][N];
int dis[N << 2];
struct node {
  int x, d;
  node(int a = 0, int b = 0) { x = a, d = b; }
  bool operator<(const node &rhs) const { return d > rhs.d; }
};
priority_queue<node> q;
inline void add0(int a, int b, int c, int d) {
  if (c & 1) c++;
  if (d & 1) d--;
  if (c > d) return;
  A[++cnt].to = b, A[cnt].l = c, A[cnt].r = d, A[cnt].id = cnt;
  S[0][a].push_back(A[cnt]);
  if (a == 1 && !c)
    dis[cnt] = c, q.push(node(cnt, dis[cnt]));
  else
    dis[cnt] = inf;
}
inline void add1(int a, int b, int c, int d) {
  if (!(c & 1)) c++;
  if (!(d & 1)) d--;
  if (c > d) return;
  A[++cnt].to = b, A[cnt].l = c, A[cnt].r = d, A[cnt].id = cnt;
  S[1][a].push_back(A[cnt]);
  if (a == 1 && !c)
    dis[cnt] = c, q.push(node(cnt, dis[cnt]));
  else
    dis[cnt] = inf;
}
int main() {
  int n = read(), m = read();
  if (n == 1) return puts("0"), 0;
  for (register int i = 1; i <= m; ++i) {
    int a = read(), b = read(), c = read(), d = read() - 1;
    add0(a, b, c, d);
    add1(a, b, c, d);
    add0(b, a, c, d);
    add1(b, a, c, d);
  }
  for (register int i = 1; i <= n; ++i) {
    sort(S[0][i].begin(), S[0][i].end()), sort(S[1][i].begin(), S[1][i].end());
  }
  int x, xd, y;
  vector<edge>::iterator it;
  while (!q.empty()) {
    x = q.top().x, xd = q.top().d, q.pop();
    if (xd != dis[x]) continue;
    if (A[x].l & 1) {
      y = A[x].to, xd++;
      while (S[0][y].size()) {
        it = --S[0][y].end();
        if (it->l > A[x].r + 1) break;
        if (it->r < xd) {
          S[0][y].pop_back();
          continue;
        }
        if (dis[it->id] > max(it->l, xd)) {
          q.push(node(it->id, dis[it->id] = max(it->l, xd)));
        }
        S[0][y].pop_back();
      }
    } else {
      y = A[x].to, xd++;
      while (S[1][y].size()) {
        it = --S[1][y].end();
        if (it->l > A[x].r + 1) break;
        if (it->r < xd) {
          S[1][y].pop_back();
          continue;
        }
        if (dis[it->id] > max(it->l, xd)) {
          q.push(node(it->id, dis[it->id] = max(it->l, xd)));
        }
        S[1][y].pop_back();
      }
    }
  }
  int ans = inf;
  for (register int i = 1; i <= cnt; ++i)
    if (A[i].to == n) Min(ans, dis[i]);
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans + 1 << endl;
  return 0;
}

```