#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();
}
