#include <bits/stdc++.h>

using namespace std;

#define int long long
// #define double long double
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, a, b) for(int i = (a); i > (b); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c)                                                                \
    sort(All(c));                                                              \
    for(bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n)                                                             \
    vector<int> od(n);                                                         \
    iota(All(od), 0LL);

template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CCIN(...)                                                              \
    char __VA_ARGS__;                                                          \
    CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
    double __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    long long __VA_ARGS__;                                                     \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}

// generic lambdas
template <typename F>
#if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
[[nodiscard]]
#elif defined(__GNUC__) &&                                                     \
    (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)
__attribute__((warn_unused_result))
#endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard)
    static inline constexpr decltype(auto)
    fix(F &&f) noexcept {
    return [f = std::forward<F>(f)](auto &&... args) {
        return f(f, std::forward<decltype(args)>(args)...);
    };
}

template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238

ll N, M;
struct edge {
    ll to, cost;
    bool operator<(const edge &another) const { return cost < another.cost; };
    bool operator>(const edge &another) const { return cost > another.cost; };
};
vector<vector<edge>> g;
VL d;

void dijkstra(ll s) {
    priority_queue<edge, vector<edge>, greater<edge>> pq;
    d.assign(10 * M, LINF);
    d[s] = 0;
    pq.push({s, 0});

    while(!pq.empty()) {
        edge now = pq.top();
        pq.pop();
        ll v = now.to;
        if(d[v] < now.cost) continue;

        REP(i, g[v].size()) {
            edge e = g[v][i];
            if(d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                pq.push({e.to, d[e.to]});
            }
        }
    }
}

signed main() {
    cin >> N >> M;
    VPL vs;
    VL ps(M), qs(M), cs(M);
    bool judge = false;
    REP(i, M) {
        LCIN(p, q, c);
        p--, q--, c--;
        ps[i] = p, qs[i] = q, cs[i] = c;
        vs.emplace_back(p, c);
        vs.emplace_back(p, -1);
        vs.emplace_back(q, c);
        vs.emplace_back(q, -1);
        if(p == 0 || q == 0) judge = true;
    }
    if(!judge) eputs(-1);
    sort(All(vs));
    vs.erase(unique(All(vs)), vs.end());
    g.resize(10 * M);
    REP(i, M) {
        ll p = lower_bound(All(vs), PL(ps[i], cs[i])) - vs.begin();
        ll pp = lower_bound(All(vs), PL(ps[i], -1)) - vs.begin();
        ll q = lower_bound(All(vs), PL(qs[i], cs[i])) - vs.begin();
        ll qq = lower_bound(All(vs), PL(qs[i], -1)) - vs.begin();
        g[p].push_back({q, 0});
        g[q].push_back({p, 0});
        g[q].push_back({qq, 0});
        g[qq].push_back({q, 1});
        g[p].push_back({pp, 0});
        g[pp].push_back({p, 1});
    }
    dijkstra(0);
    ll idx = lower_bound(All(vs), PL{N - 1, 0}) - vs.begin();
    ll ans = LINF;
    while(idx != vs.size()) {
        ans = min(ans, d[idx++]);
    }
    cout << (ans == LINF ? -1 : ans) << "\n";
}

Measured memory_bytes: 117092