#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<bitset>
#include<unordered_map>
using namespace std;
//---------------------------------------------------
//ライブラリゾーン！！！！
typedef long long ll;
typedef long double ld;
#define str string
#define rep(i,j) for(ll i=0;i<(long long)(j);i++)
#define all(a) (a).begin(),(a).end()
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = { { 0,0,-1,1 },{ -1,1,0,0 } };
struct P {
	ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B {//隣接リスト表現
	ll to, cost;
};
struct E {//辺の情報を入れる変数
	ll from, to, cost;
};
bool operator<(E a, E b) {
	return a.cost < b.cost;
}
struct H {
	ll x, y;
};
bool operator<(H a, H b) {
	if (a.x != b.x) return a.x < b.x;
	return a.y < b.y;
}
bool operator>(H a, H b) {
	if (a.x != b.x) return a.x > b.x;
	return a.y > b.y;
}
bool operator==(H a, H b) {
	return a.x == b.x&&a.y == b.y;
}
bool operator!=(H a, H b) {
	return a.x != b.x || a.y != b.y;
}
ll gcm(ll i, ll j) {//最大公約数
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcm(j%i, i);
}
ld rad(H a, H b) {
	return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}//rad＝座標上の2点間の距離
ll ari(ll a, ll b, ll c) {
	return (a + b)*c / 2;
}//等差数列の和
ll fact(ll x, ll k, ll p) {//最大値、個数、あまり
	ll sum = 1;
	for (int i = 0; i < k; i++) {
		sum *= (x--);
		sum %= p;
	}
	return sum;
}//階乗(正）
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res*x%p;
		x = x*x%p;
		n >>= 1;
	}
	return res;
}//x^n%p
int ctoi(char a) {
	return (int)a - '0';
}
#define int long long
const long long Inf = 4523372036854775807;
const int inf = 15000000000;
//----------------------------------------------------
//++++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m;
int a[200000], b[200000], c[200000];
vector<int>id[100001];
vector<int>by[100001];
vector<B>e[1000001];
int d[1000001];
signed main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
		by[a[i]].push_back(c[i]);
		by[b[i]].push_back(c[i]);
	}
	int cid = n + 1;
	for (int i = 1; i <= n; i++) {
		sort(all(by[i]));
		by[i].erase(unique(all(by[i])), by[i].end());
		id[i].resize(by[i].size());
		for (int j = 0; j < by[i].size(); j++) {
			id[i][j] = cid++;
		}
	}
	for (int i = 0; i < m; i++) {
		int ak = lower_bound(all(by[a[i]]), c[i]) - by[a[i]].begin();
		int bk = lower_bound(all(by[b[i]]), c[i]) - by[b[i]].begin();
		int aid = id[a[i]][ak], bid = id[b[i]][bk];
		e[aid].push_back(B{ bid,0 });
		e[bid].push_back(B{ aid,0 });
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < id[i].size(); j++) {
			e[i].push_back(B{ id[i][j],1 });
			e[id[i][j]].push_back(B{ i,0 });
		}
	}
	priority_queue<P, vector<P>, greater<P>>p;
	for (int i = 2; i <= 1000000; i++)
		d[i] = inf;
	p.push(P{ 1,0 });
	while (!p.empty()) {
		P t = p.top(); p.pop();
		if (t.cost != d[t.pos]) continue;
		for (int i = 0; i < e[t.pos].size(); i++) {
			if (d[e[t.pos][i].to] > d[t.pos] + e[t.pos][i].cost) {
				d[e[t.pos][i].to] = d[t.pos] + e[t.pos][i].cost;
				p.push(P{ e[t.pos][i].to,d[e[t.pos][i].to] });
			}
		}
	}
	if (d[n] == inf) cout << -1 << endl;
	else cout << d[n] << endl;
	getchar(); getchar();
}

Measured memory_bytes: 88820