#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n, m;
int p[200100], q[200100], c[200100];

const int MAX = 2000100;
struct edge {
    int to, cost;
};

vector<edge> G[MAX];
int d[MAX];

void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P> > que;
    for (int i = 0; i < MAX; i++) d[i] = inf;
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

vector<P> e[1000100];
set<int> s[1000100];
vector<int> idx[100100];

int solve() {
    for (int i = 0; i < m; i++) {
        e[c[i]].push_back(P(p[i], q[i]));
        s[c[i]].insert(p[i]);
        s[c[i]].insert(q[i]);
    }
    int buf = 0;
    for (int i = 1; i <= 1000000; i++) {
        int L = (int)e[i].size();
        map<int, int> m;
        for (auto v : s[i]) {
            m[v] = buf;
            idx[v].push_back(buf);
            buf++;
        }
        for (int j = 0; j < L; j++) {
            P p = e[i][j];
            int a = m[p.first], b = m[p.second];
            G[a].push_back(edge{b, 0});
            G[b].push_back(edge{a, 0});
        }
    }
    for (int i = 1; i <= n; i++) {
        int L = (int)idx[i].size();
        for (int j = 0; j < L; j++) {
            G[idx[i][j]].push_back(edge{buf, 1});
            G[buf].push_back(edge{idx[i][j], 0});
        }
        buf++;
    }
    int s = buf;
    for (int i = 0; i < (int)idx[1].size(); i++) {
        G[s].push_back(edge{idx[1][i], 1});
    }
    
    if ((int)idx[1].size() == 0) return -1;
    dijkstra(s);
    int res = inf;
    for (int i = 0; i < (int)idx[n].size(); i++) {
        res = min(res, d[idx[n][i]]);
    }
    return (res < inf ? res : -1);
}

void input() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) cin >> p[i] >> q[i] >> c[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}


Measured memory_bytes: 177216