#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair


const int INF = INT_MAX;


template <typename T>
void dij1(int nV, vvii& adjacent, vector<T>& d, int start) {
	// nV: |V|
	d.assign(nV, INF);
	d[start] = 0;
	vector<int> prev(nV, -1);

	priority_queue<pair<T, int>> Q;
	Q.push(make_pair(0, start));

	vector<bool> visited(nV, false);

	while (!Q.empty()) {
		T du = -Q.top().first;
		int u = Q.top().second; Q.pop();
		if (visited[u]) continue;
		visited[u] = true;

		int c = adjacent[u].size();
		for (int i=0; i<c; ++i) {
			int v = adjacent[u][i].first;
			T distance = adjacent[u][i].second;
			T alt = du + distance;
			if (d[v] > alt) {
				d[v] = alt;
				prev[v] = u;
				Q.push(make_pair(-alt, v));
			}
		}
	}
}

// template <typename T>
// void dij(int nV, vector<vector<pair<int,T>>>& adjacent, vector<vector<T>>& d) {
// 	d.resize(nV);
// 	for (int start=0; start<nV; ++start) {
// 		dij1(nV, adjacent, d[start], start);
// 	}
// }


ll solve(int N, int M, vi& p, vi& q, vi& c) {
    vector<ii> table; // table[<node>] = ii(p,c)
	table.reserve(N + M*2);
    map<ii,int> tableR; // tableR[ii(p,c)] = <node

    vvii nxt;
	int node_id = 0;

	rep(st,N) {
		ii pc(st, -1);
		table.pb(pc);
		tableR[pc] = node_id++;
        nxt.pb(vii());
	}

    rep(i,M){
        ii pc(p[i], c[i]), qc(q[i], c[i]);

		ii p_1(p[i], -1), q_1(q[i], -1);
		int p_1D = tableR[p_1], q_1D = tableR[q_1];

        int pcD, qcD;
        if (found(tableR, pc)) {
            pcD = tableR[pc];
        } else {
            table.pb(pc);
            pcD = tableR[pc] = node_id++;
            nxt.pb(vii());
			nxt[pcD].pb(ii(p_1D, 0));
			nxt[p_1D].pb(ii(pcD, 1));
        }
        if (found(tableR, qc)) {
            qcD = tableR[qc];
        } else {
            table.pb(qc);
            qcD = tableR[qc] = node_id++;
            nxt.pb(vii());
			nxt[qcD].pb(ii(q_1D, 0));
			nxt[q_1D].pb(ii(qcD, 1));
        }

        nxt[pcD].pb(ii(qcD, 0));
        nxt[qcD].pb(ii(pcD, 0));
    }

    int nV = nxt.size();

#ifdef DEBUG
    rep(st, N) {
        cerr << "[" << st << "] " << cs[st] << endl;
    }
    rep(v, nV) {
        cerr << v << ") " << nxt[v] << endl;
    }
#endif
	int start = tableR[ii(0, -1)], goal = tableR[ii(N-1, -1)];

    vector<ll> d(nV, INF);
    dij1(nV, nxt, d, start);
#ifdef DEBUG
    cerr << d << endl;
#endif
    return (d[goal] == INF) ? -1 : d[goal];
}

int main() {
    int N, M; cin >> N >> M;
    vi u(M), v(M), c(M);
    rep(i,M) {
        int ui, vi, ci; cin >> ui >> vi >> ci;
        --ui; --vi; --ci;
        u[i] = ui; v[i] = vi; c[i] = ci;
    }
    cout << solve(N,M,u,v,c) << endl;
    return 0;
}


Measured memory_bytes: 79912