#include<cstdio>
#include<cctype>
#include<unordered_map>
#include<vector>
#include<queue>
using namespace std;
const int N=1e5,M=2e5,P=N+2*M,inf=0x3f3f3f3f;
typedef pair<int,int> pii;
int n,m,tot;
vector<pii>e[P+1];
int dis[P+1];
int read()
{
	int ret=0;
	char c=getchar();
	while(!isdigit(c)) {
		c=getchar();
	}
	while(isdigit(c)) {
		ret=ret*10+c-'0';
		c=getchar();
	}
	return ret;
}
int get_number(int p,int c)
{
	static unordered_map<long long,int>mp;
	long long has=(long long)p<<32|c;
	if(!mp[has]) {
		mp[has]=++tot;
	}
	return mp[has];
}
void dijkstra()
{
	static bool vis[P+1];
	static priority_queue<pii,vector<pii>,greater<pii>>que;
	fill(dis+1,dis+tot+1,inf);
	que.push(pii{dis[1]=0,1});
	while(!que.empty()) {
		int u=que.top().second;
		que.pop();
		if(vis[u]) {
			continue;
		}
		vis[u]=true;
		for(pii e:e[u]) {
			int v=e.first;
			if(dis[u]+e.second<dis[v]) {
				que.push(pii{dis[v]=dis[u]+e.second,v});
			}
		}
	}
	return;
}
int main()
{
	n=read(),m=read();
	tot=n;
	for(int i=1,u,v,c;i<=m;++i) {
		u=read(),v=read(),c=read();
		int uc=get_number(u,c),vc=get_number(v,c);
		e[u].push_back(pii{uc,1}),e[uc].push_back(pii{u,1});
		e[v].push_back(pii{vc,1}),e[vc].push_back(pii{v,1});
		e[uc].push_back(pii{vc,0}),e[vc].push_back(pii{uc,0});
	}
	dijkstra();
	printf("%d\n",dis[n]==inf?-1:dis[n]/2);
	return 0;
}

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

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
        #define eprintf(...) 42
#endif

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(x) (x).begin(),(x).end()
#define foreach(u,v) for(auto (u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL<<60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
*/

typedef tuple<int, int, int> triple;

int n, m;
map<pii, vector<triple>> g;
map<pii, ll> dist;

int main()
{
  cin >> n >> m;
  rep(i, m){
    int p, q, c;
    cin >> p >> q >> c;
    p--; q--;

    g[mp(p, c)].pb(mt(q, c, 0));
    g[mp(q, c)].pb(mt(p, c, 0));
    g[mp(p, c)].pb(mt(q, -1, 0));
    g[mp(q, c)].pb(mt(p, -1, 0));
    g[mp(p, -1)].pb(mt(q, c, 1));
    g[mp(q, -1)].pb(mt(p, c, 1));

    dist[mp(p, c)] = linf;
    dist[mp(q, c)] = linf;
    dist[mp(p, -1)] = linf;
    dist[mp(q, -1)] = linf;
  }
  
  rep(i, n) dist[mp(i, -1)] = linf;

  priority_queue<triple, vector<triple>, greater<triple>> que;
  que.push(mt(0, 0, -1));
  dist[mp(0, -1)] = 0;

  while(!que.empty()){
    int d, v, c;
    tie(d, v, c) = que.top();
    que.pop();

    if(dist[mp(v, c)] < d) continue;
    for(auto e : g[mp(v, c)]){
      int to, cc, dd;
      tie(to, cc, dd) = e;
      if(dist[mp(to, cc)] > dist[mp(v, c)] + dd){
        dist[mp(to, cc)] = dist[mp(v, c)] + dd;
        que.push(mt(dist[mp(to, cc)], to, cc));
      }
    }
  }
  
  ll ans = dist[mp(n-1, -1)];
  if(ans == linf) cout << -1 << endl;
  else cout << ans << endl;

  return 0;
}
