#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
using namespace std;
#define INF 0x3f3f3f3f
//#define INF 1100000000000000000LL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=1050;
//int arr[SIZE]={};
//ここまでテンプレ
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void shortestPath(const Graph &g, int s,
    vector<Weight> &dist, vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF); dist[s] = 0;
  prev.assign(n, -1);
  priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
    Edge e = Q.top(); Q.pop();
    if (prev[e.dst] != -1) continue;
    prev[e.dst] = e.src;
    FOR(f,g[e.dst]) {
      if (dist[f->dst] > e.weight+f->weight) {
        dist[f->dst] = e.weight+f->weight;
        Q.push(Edge(f->src, f->dst, e.weight+f->weight));
      }
    }
  }
}
vector<int> buildPath(const vector<int> &prev, int t) {
  vector<int> path;
  for (int u = t; u >= 0; u = prev[u])
    path.push_back(u);
  reverse(path.begin(), path.end());
  return path;
}
//↓二次元→一次元座標圧縮：vector<pair<int,int>>を入れると対応関係をmap[旧座標(pair<int,int>)]=新座標(int)で返す
map<pair<int,int>,int> comp(vector<pair<int,int>> v){
	map<pair<int,int>,int> pam;
	int sz=v.size();
	for(int i=0;i<sz;i++){
		pam[v[i]]=0;
	}
	//新座標が1からほしいときはnc=1にする
	int nc=0;
	for(pair<pair<int,int>,int> i:pam){
		pam[i.first]=nc;
		nc++;
	}
	return pam;
}
//↑座標圧縮
int main(){
	int N,M;
	cin>>N>>M;
	vector<pint> V;
	vint p,q,c;
	for(int i=0;i<M;i++){
		int x,y,z;
		cin>>x>>y>>z;
		V.pb(mp(x,z));
		V.pb(mp(y,z));
		p.pb(x);
		q.pb(y);
		c.pb(z);
	}
	for(int i=1;i<=N;i++){
		V.pb(mp(i,-1));
	}
	map<pint,int> pam=comp(V);
	int sz=pam.size();
	Graph G;
	for(int i=0;i<sz;i++){
		Edges E;
		G.pb(E);
	}
	for(int i=0;i<M;i++){
		int a,b,x,y;
		a=pam[mp(p[i],c[i])];
		b=pam[mp(q[i],c[i])];
		x=pam[mp(p[i],-1)];
		y=pam[mp(q[i],-1)];
		G[a].pb(Edge(a,b,0));
		G[b].pb(Edge(b,a,0));
		G[a].pb(Edge(a,x,0));
		G[b].pb(Edge(b,y,0));
		G[x].pb(Edge(x,a,1));
		G[y].pb(Edge(y,b,1));
	}
	vector<Weight> dist;
	vint prev;
	shortestPath(G,pam[mp(1,-1)],dist,prev);
	if(dist[pam[mp(N,-1)]]==INF){
		cout<<-1<<endl;
		return 0;
	}
	cout<<dist[pam[mp(N,-1)]]<<endl;
	return 0;
}

Measured memory_bytes: 78244