// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define mp make_pair

#define pb push_back

#define pii pair<int,int>

#define A first

#define B second

#define db double

#define lowbit(p) (p&(-p))

using namespace std;

inline void read(int &x){

	x=0; char c=getchar(); int p=1;

	for (;c<48;c=getchar())if (c=='-')p=-1;

	for (;c>47;c=getchar())x=(x<<1)+(x<<3)+(c^48);

	x*=p;

}

#define M 100005

map< int,int >id[M];

struct ed{

	int x,l,nx;

}e[M*12];

int nx[6*M],dis[6*M],n,m,ecnt=1;

bool mark[6*M];

int tot,r[2],Q[2][6*M];

inline void add(int x,int y,int l){

	e[ecnt]=(ed){y,l,nx[x]};

	nx[x]=ecnt++;

}

void bfs(int s,int t){

	int x,now=0,i,tmp;

	memset(dis,63,sizeof(dis));

	dis[s]=0;

	Q[now][++r[now]]=s;

	

	for (;r[now];now=!now){

		for (;r[now];){

			x=Q[now][r[now]--];

			if (mark[x])continue;

			mark[x]=1;

			if (x==t){

				printf("%d\n",dis[t]);

				return ;

			}

			for (i=nx[x];i;i=e[i].nx){

				if (dis[x]+e[i].l<dis[e[i].x]){

					dis[e[i].x]=dis[x]+e[i].l;

					tmp=now^e[i].l;

					Q[tmp][++r[tmp]]=e[i].x;

				}

			}

		}

	}

	printf("-1\n");

}

inline int cal(int x,int col){

	int &res=id[x][col];

	if (res)return res;

	res=++tot;

	add(x,res,1);

	add(res,x,0);

	return res; 

}

int main(){

	int Caseid;

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

//	read(Caseid);

	read(n); read(m);	

	int i,x,y,c;

	tot=n;

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

		read(x); read(y); read(c);

		x=cal(x,c);

		y=cal(y,c);

		add(x,y,0);

		add(y,x,0);

	}

	bfs(1,n);

	return 0;

}

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

using namespace std;



const int N = 1e5 + 10, M = 2e5 + 30;

int n, m;

struct edge {

    int y, nxt, id;

}e[M << 1];

int lin[M], len;

int dis[N], pos[N];

bool vis[N];

set<int> c[N];



inline int read() {

    int s = 0, f = 1;

    char ch;

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

    for(; ch >= '0' && ch <= '9'; ch = getchar())   s = (s << 1) + (s << 3) + ch - '0';

    return s * f;

}



inline void add(int xx, int yy, int vv) {

    e[++len] = (edge){yy, lin[xx], vv};

    lin[xx] = len;

}



inline void Dijkstra() {

    priority_queue< pair < int, int > > q;

    memset(dis, 0x3f, sizeof(dis));

    memset(vis, 0, sizeof(vis));

    dis[1] = 0;

    q.push(make_pair(0, 1));

    while(!q.empty()) {

        int x = q.top().second; q.pop();

        if(vis[x])  continue;

        vis[x] = 1;

        for(int i = lin[x]; i; i = e[i].nxt) {

            int y = e[i].y, v = c[x].count(e[i].id) ? 0 : 1;

            if(dis[y] > dis[x] + v) {

                dis[y] = dis[x] + v;

                q.push(make_pair(-dis[y], y));

                c[y].clear();

                c[y].insert(e[i].id);

            } else if(dis[y] == dis[x] + v) c[y].insert(e[i].id);

        }

    }

}



int main() {

    n = read(), m = read(); 

    if(m == 0) { puts("-1"); return 0; }

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

        int xx = read(), yy = read(), vv = read();

        add(xx, yy, vv);

        add(yy, xx, vv);

    }

    Dijkstra();

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

    return 0;

}