#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
struct IO {
  static const int maxn = (1 << 25) + 10;
  char a[maxn], *s, b[maxn], *t;
  void INPUT() {
    s = a;
    t = b;
    a[fread(a, 1, sizeof a, stdin)] = 0;
  }
  void OUTPUT() { fwrite(b, 1, t - b, stdout); }
  operator int() {
    int x = 0;
    while (*s != '-' && (*s < '0' || *s > '9')) {
      ++s;
    }
    bool f = 0;
    if (*s == '-') {
      f = 1;
      ++s;
    }
    while (*s >= '0' && *s <= '9') {
      (x *= 10) += *s - '0';
      ++s;
    }
    if (f) {
      x = -x;
    }
    return x;
  }
  operator long long() {
    long long x = 0;
    while (*s != '-' && (*s < '0' || *s > '9')) {
      ++s;
    }
    bool f = 0;
    if (*s == '-') {
      f = 1;
      ++s;
    }
    while (*s >= '0' && *s <= '9') {
      (x *= 10) += *s - '0';
      ++s;
    }
    if (f) {
      x = -x;
    }
    return x;
  }
  operator char() {
    while (*s <= 32) {
      ++s;
    }
    char ret = *s;
    ++s;
    return ret;
  }
  inline void out(int x) {
    if (!x) {
      *t++ = '0';
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      int y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    return;
  }
  inline void out(int x, char C) {
    if (!x) {
      *t++ = '0';
      *t++ = C;
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      int y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    *t++ = C;
    return;
  }
  inline void out(long long x) {
    if (!x) {
      *t++ = '0';
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      long long y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    return;
  }
  inline void out(long long x, char C) {
    if (!x) {
      *t++ = '0';
      *t++ = C;
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      long long y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    *t++ = C;
    return;
  }
  inline void out(char c) {
    *t++ = c;
    return;
  }
  inline void out(char *s) {
    while (*s >= ' ') {
      *t++ = *s++;
    }
    return;
  }
} io;
void Main();
int main(int argc, char *argv[]) {
  io.INPUT();
  Main();
  io.OUTPUT();
  return 0;
}
const int maxn = 5e5 + 100, maxm = 5e5 + 100;
int n, m;
int u[maxm][2], l[maxm], r[maxm], dp[maxm << 1][2], iter[maxn][2];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    heap;
vector<int> g[maxn];
void Main() {
  n = io;
  m = io;
  if (n == 1) {
    io.out(0, '\n');
    return;
  }
  memset(dp, 0x3f, sizeof dp);
  for (int i = 0, i_end_ = (m); i < i_end_; ++i) {
    u[i][0] = io;
    u[i][1] = io;
    l[i] = io;
    r[i] = io;
    g[u[i][0]].push_back((i << 1) | 1);
    g[u[i][1]].push_back(i << 1);
    if (!l[i]) {
      if (u[i][0] == 1) {
        dp[i << 1][0] = 0;
        heap.push(make_pair(0, i << 1));
      }
      if (u[i][1] == 1) {
        dp[(i << 1) | 1][0] = 0;
        heap.push(make_pair(0, (i << 1) | 1));
      }
    }
  }
  for (int u = (1), u_end_ = (n); u <= u_end_; ++u) {
    sort((g[u]).begin(), (g[u]).end(),
         [&](int i, int j) { return l[i >> 1] < l[j >> 1]; });
  }
  int ans = 1000000001;
  while (!heap.empty()) {
    int T = heap.top().first, e = heap.top().second, v = u[e >> 1][e & 1],
        d = T & 1, lst = T;
    heap.pop();
    if (T != dp[e][d]) {
      continue;
    }
    if (v == n) {
      chkmin(ans, T);
    }
    while (iter[v][d] < (int)((g[v]).size())) {
      int i = g[v][iter[v][d]];
      int l = ::l[i >> 1], r = ::r[i >> 1];
      if (lst < l) {
        break;
      }
      ++iter[v][d];
      int w = l;
      if (w - T & 1) {
        ++w;
      }
      chkmax(w, T);
      if (w + 1 <= r) {
        if (chkmin(dp[i][d ^ 1], w + 1)) {
          heap.push(make_pair(w + 1, i));
        }
      }
      if (w <= r) {
        if (chkmin(dp[i ^ 1][d], w)) {
          heap.push(make_pair(w, i ^ 1));
        }
      }
      chkmax(lst, (r - lst & 1) ? r - 1 : r);
    }
  }
  io.out(ans <= 1000000000 ? ans : -1, '\n');
  return;
}
