#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <stack>
#include <limits>
#include <climits>
#include <cassert>
#include <fstream>
#include <cstring>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <ciso646>
#include <set>
#include <array>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define VS vector<string>
#define VI vector<ll>
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define pii pair<ll,ll>
#define pcc pair<char,char>
#define pic pair<ll,char>
#define pci pair<char,ll>
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
	struct input_returnner {
		ll N; input_returnner(ll N_ = 0) :N(N_) {}
		template<typename T> operator vector<T>() const { vector<T> res(N); for (auto &a : res) cin >> a; return std::move(res); }
		template<typename T> operator T() const { T res; cin >> res; return res; }
		template<typename T> T operator - (T right) { return T(input_returnner()) - right; }
		template<typename T> T operator + (T right) { return T(input_returnner()) + right; }
		template<typename T> T operator * (T right) { return T(input_returnner()) * right; }
		template<typename T> T operator / (T right) { return T(input_returnner()) / right; }
		template<typename T> T operator << (T right) { return T(input_returnner()) << right; }
		template<typename T> T operator >> (T right) { return T(input_returnner()) >> right; }
	};
	template<typename T> input_returnner in() { return in<T>(); }
	input_returnner in() { return input_returnner(); }
	input_returnner in(ll N) { return std::move(input_returnner(N)); }
}

ll MOD = 1e9 + 7;

void solve();

signed main() {
	SETUP;
	solve();
#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}

#define int ll

struct Edge {
	int to;
	int c;
};

vector<vector<pii> > Graph;// Graph[from] = {(to, company)}
unordered_map<int, unordered_map<int, vector<int> > > cGraph; //cGraph[c][from] = {to}
set<tuple<int, int, int>> memo; //from, to, company (from < to)
vector<int> dists;
queue<int> que;
vector<int> visited;
set<pii> DFSmemo; //node, company 

bool check(int node, int to, int company) {
	if (node > to) swap(node, to);
	return memo.count(make_tuple(node, to, company)) == 0;
}

void DFS(int node, int company, int dist) {
	if (DFSmemo.count(make_pair(node, company)) == 1) {
		return;
	}
	DFSmemo.emplace(node, company);
	static int cnt = 0;
	//cerr << "message:" << ++cnt << " " << node << " "<< company << endl;
	if (not visited[node]) {
		que.push(node);
		visited[node] = true;
	}
	for (auto &to : cGraph[company][node]) {
		if (dists[to] == -1) dists[to] = dist;
		if (check(node, to, company)) {
			memo.emplace(node, to, company);
			DFS(to, company, dist);
		}
	}
}

void solve() {
	int N, M; cin >> N >> M;
	Graph.resize(N);
	visited.resize(N);
	dists = vector<int>(N, -1);
	REP(i, M) {
		int p, q, c; cin >> p >> q >> c;
		--p; --q; --c;
		Graph[p].emplace_back(q, c);
		Graph[q].emplace_back(p, c);
		cGraph[c][p].push_back(q);
		cGraph[c][q].push_back(p);
	}

	que.push(0);
	visited[0] = true;
	dists[0] = 0;
	while (not que.empty()) {
		int cur = que.front(); que.pop();
		for (auto &a : Graph[cur]) {
			DFS(cur, a.second, dists[cur]+1);
			if (dists[N - 1] != -1) {
				cout << dists[N - 1] << endl;
				return;
			}
		}
	}
	cout << dists[N - 1] << endl;
}

Measured memory_bytes: 107976