// Slower C++ solution:
/*

  mail: mleautomaton@foxmail.com

  author: MLEAutoMaton

  This Code is made by MLEAutoMaton

*/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<math.h>

#include<algorithm>

#include<queue>

#include<set>

#include<map>

#include<iostream>

using namespace std;

#define ll long long

#define REP(a,b,c) for(int a=b;a<=c;a++)

#define re register

#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)

inline int gi(){

	int f=1,sum=0;char ch=getchar();

	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}

	return f*sum;

}

const int N=200010;

int f[N],n,m;

int dis[N],vis[N],front[N],cnt;

struct node{int to,nxt,w;}e[N<<1];

set<int>se[N];

queue<int>Q;

void Add(int u,int v,int w){e[++cnt]=(node){v,front[u],w};front[u]=cnt;}

int find(int x){if(f[x]!=x)f[x]=find(f[x]);return f[x];}

void SPFA(){

	Q.push(1);memset(dis,63,sizeof(dis));dis[1]=0;

	while(!Q.empty()){

		int u=Q.front();Q.pop();vis[u]=0;

		for(int i=front[u];i;i=e[i].nxt){

			int v=e[i].to,W;

			if(se[u].find(e[i].w)!=se[u].end())W=0;

			else W=1;

			if(dis[v]>dis[u]+W){

				dis[v]=dis[u]+W;se[v].clear();se[v].insert(e[i].w);

				if(!vis[v]){vis[v]=1;Q.push(v);}

			}

			else if(dis[v]==dis[u]+W){if(se[v].find(e[i].w)==se[v].end()){if(!vis[v]){vis[v]=1;Q.push(v);}}se[v].insert(e[i].w);}

		}

	}

}

int main(){

	n=gi();m=gi();

	for(int i=1;i<=n;i++)f[i]=i;

	for(int i=1;i<=m;i++){

		int u=gi(),v=gi(),w=gi();

		f[find(u)]=find(v);Add(u,v,w);Add(v,u,w);

	}

	if(find(1)!=find(n))return puts("-1"),0;

	SPFA();printf("%d\n",dis[n]==dis[n+1]?-1:dis[n]);

	return 0;

}

//%HYJ,Not Good!!!

//% M _ s e a, G o o d ! ! !

//%xgzc,sb!

// Faster C++ solution for the same problem (measured speedup: 1.2977050624633637x):
#include <bits/stdc++.h>

#define sc(x) scanf("%d", &x)

#define pr(x) printf("%d\n", x)

#define lowbit(x) (x & -x)

#define ll long long

#define N 500010

using namespace std;

ll tot = 0, head[N], v[N], dis[N], n, m, cnt;

struct Xiao

{

	int next, to, w;

}f[N * 3];

void add(int x, int y, int z)

{

	f[++tot].to = y;

	f[tot].next = head[x];

	f[tot].w = z;

	head[x] = tot;

}

void spfa()

{

	memset(dis, 127, sizeof dis);

	memset(v, 0, sizeof v);

	queue <int> q;

	q.push(1);

	v[1] = 1;

	dis[1] = 0;

	while (q.size())

	{

		int x = q.front();

		q.pop();

		for (int i = head[x]; i; i = f[i].next)

		{

			int y = f[i].to;

			if (dis[y] > dis[x] + f[i].w)

			{

				dis[y] = dis[x] + f[i].w;

				if (!v[y])

				{

					v[y] = 1;

					q.push(y);

				}

			}

		}

		v[x] = 0;

	}

}

map < pair <int, int>, int > p;

int get(int x, int y)

{

	if (p.find(make_pair(x, y)) != p.end()) return p[make_pair(x, y)];

	else return p[make_pair(x, y)] = ++cnt;

}

int main()

{

	//freopen("railway.in", "r", stdin);

	//freopen("railway.out", "w", stdout);

	sc(n);

	sc(m);

	cnt = n;

	for (int i = 1; i <= m; i++)

	{

		int x, y, z;

		sc(x);

		sc(y);

		sc(z);

		int p1 = get(x, z);

		int p2 = get(y, z);

		add(p2, p1, 0);

		add(p1, p2, 0);

		add(x, p1, 1);

		add(p1, x, 1);

		add(y, p2, 1);

		add(p2, y, 1);

	}

	spfa();

	printf("%lld\n", dis[n] > 2 * n ? -1 : dis[n] / 2);

	return 0;

}