#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;
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define MN 301000
#define MM 1601000

typedef std::pair<int, int> P;
int h[MN], nxt[MM], to[MM], cost[MM], K = 0;
std::vector<P> v[101000];
int d[MN], n, m;
std::deque<int> q;

void ins(int u, int v, int c) {nxt[++K] = h[u]; h[u] = K; to[K] = v; cost[K] = c;}

int bfs()
{
	q.push_back(1);
	memset(d, 0x3f, sizeof(d)); d[1] = 0;
	while(!q.empty())
	{
		int u = q.front(); q.pop_front();
		for(int i = h[u]; i; i = nxt[i])
		{
			if(d[to[i]] > d[u] + cost[i])
			{
				d[to[i]] = d[u] + cost[i];
				if(cost[i]) q.push_back(to[i]);
				else q.push_front(to[i]);
			}
		}
	}
	if(d[n] > 1e9) return -1;
	return d[n];
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; i++)
	{
		int p, q, c; scanf("%d%d%d", &p, &q, &c);
		v[p].push_back(P(c, i));
		v[q].push_back(P(c, i));
		ins(p, n + i, 1); ins(n + i, p, 0);
		ins(q, n + i, 1); ins(n + i, q, 0);
	}
	for(int i = 1; i <= n; i++)
	{
		std::sort(v[i].begin(), v[i].end());
		int S = v[i].size();
		for(int j = 1; j < S; j++)
		{
			if(v[i][j].first == v[i][j - 1].first)
			{
				ins(v[i][j].second + n, v[i][j - 1].second + n, 0);
				ins(v[i][j - 1].second + n, v[i][j].second + n, 0); 
			}
		}
	}
	printf("%d\n", bfs());
}