// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <algorithm>

#include <climits>

#include <cstring>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <vector>

#include <list>

#define rep(i,m,n) for(i=m;i<=n;i++)

#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)

#define mod 1000000007

#define inf 0x3f3f3f3f

#define vi vector<int>

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define ll long long

#define pi acos(-1.0)

#define pii pair<int,int>

#define Lson L, mid, rt<<1

#define Rson mid+1, R, rt<<1|1

const int maxn=1e5+10;

using namespace std;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}

ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int n,m,k,t,h[maxn],tot,ans[maxn];

set<int>comp[maxn];

struct node

{

    int to,nxt,com;

}e[maxn<<2];

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

{

    tot++;

    e[tot].to=y;

    e[tot].com=z;

    e[tot].nxt=h[x];

    h[x]=tot;

}

priority_queue<pair<int,int> >p;

int main()

{

    int i,j;

    memset(ans,inf,sizeof ans);

    scanf("%d%d",&n,&m);

    while(m--)

    {

        int a,b,c;

        scanf("%d%d%d",&a,&b,&c);

        add(a,b,c);

        add(b,a,c);

    }

    p.push({0,1});ans[1]=0;

    while(!p.empty())

    {

        int now=p.top().se,ca=-p.top().fi;

        p.pop();

        if(ans[now]<ca)continue;

        for(i=h[now];i;i=e[i].nxt)

        {

            int to=e[i].to,to_com=e[i].com;

            int to_ca=ca+(!comp[now].count(to_com));

            if(ans[to]>to_ca)

            {

                ans[to]=to_ca;

                p.push({-to_ca,to});

                comp[to].clear();

                comp[to].insert(to_com);

            }

            else if(ans[to]==to_ca)

            {

                comp[to].insert(to_com);

            }

        }

    }

    printf("%d\n",ans[n]==inf?-1:ans[n]);

    //system("Pause");

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

int n,m,num,dis[500010],Link[500010],t=0;

bool vis[500010];

map < pair< int,int >,int> mp;

struct dsa

{

	int v,nex,w;

}e[1200010];

void Insert(int xx,int yy,int zz) 

{

	e[++t].nex=Link[xx];e[t].v=yy;e[t].w=zz;Link[xx]=t;

	e[++t].nex=Link[yy];e[t].v=xx;e[t].w=zz;Link[yy]=t;

}

void Dijkstra()

{

	priority_queue < pair<int,int> > q;

	memset(dis,10,sizeof(dis));

	dis[1]=0;

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

	while (q.size()) {

		int x=q.top().second;

		q.pop();

		if (vis[x]) continue;

		vis[x]=true;

		for (int i=Link[x];i;i=e[i].nex) 

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

		  	  dis[e[i].v]=dis[x]+e[i].w;

		  	  q.push(make_pair(-dis[e[i].v],e[i].v));

		  }

	}

}

int main()

{

	scanf("%d%d",&n,&m);

	num=n;

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

		int xx,yy,zz;

		scanf("%d%d%d",&xx,&yy,&zz);

		if (mp.find(make_pair(xx,zz))==mp.end()) mp[make_pair(xx,zz)]=++num;

		if (mp.find(make_pair(yy,zz))==mp.end()) mp[make_pair(yy,zz)]=++num;

		Insert(mp[make_pair(xx,zz)],mp[make_pair(yy,zz)],0);

		Insert(mp[make_pair(xx,zz)],xx,1);

		Insert(mp[make_pair(yy,zz)],yy,1);

	}

	Dijkstra();

	if (dis[n]==dis[0]) cout<<"-1"<<endl;else cout<<dis[n]/2;

	return 0;

}

// Fastest known solution for this problem: