// Slower C++ solution:
#include<queue>

#include<cstdio>

#include<vector>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAXM = 200000;

const int MAXN = 300000;

struct edge{

	int u, v, c;

}edges[MAXM + 5];

bool operator < (edge a, edge b) {

	return a.c < b.c;

}

vector<int>G[MAXN + 5];

void addedge(int u, int v) {

	G[u].push_back(v);

	G[v].push_back(u);

}

int vcnt, fa[MAXN + 5], con[MAXN + 5], vis[MAXN + 5];

int Find(int x) {

	if( fa[x] == x ) return x;

	return fa[x] = Find(fa[x]);

}

void Union(int x, int y) {

	int fx = Find(x), fy = Find(y);

	if( fx != fy ) fa[fx] = fy;

}

int dis[MAXN + 5];

void bfs(int s) {

	memset(dis, -1, sizeof dis);

	queue<int>que; que.push(s);

	dis[s] = 0;

	while( !que.empty() ) {

		int f = que.front(); que.pop();

		for(int i=0;i<G[f].size();i++) {

			if( dis[G[f][i]] == -1 ) {

				dis[G[f][i]] = dis[f] + 1;

				que.push(G[f][i]);

			}

		}

	}

}

int main() {

	int N, M;

	scanf("%d%d", &N, &M);

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

		scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].c);

	sort(edges+1, edges+M+1);

	vcnt = N; int lst = 1;

	for(int i=2;i<=M+1;i++) {

		if( edges[i].c != edges[i-1].c ) {

			for(int j=lst;j<i;j++) {

				fa[edges[j].u] = edges[j].u;

				fa[edges[j].v] = edges[j].v;

				con[edges[j].u] = con[edges[j].v] = 0;

				vis[edges[j].u] = vis[edges[j].v] = false; 

			}

			for(int j=lst;j<i;j++)

				Union(edges[j].u, edges[j].v);

			for(int j=lst;j<i;j++) {

				if( fa[edges[j].u] == edges[j].u )

					if( con[edges[j].u] == 0 )

						con[edges[j].u] = (++vcnt);

				if( fa[edges[j].v] == edges[j].v )

					if( con[edges[j].v] == 0 )

						con[edges[j].v] = (++vcnt);	

			}

			for(int j=lst;j<i;j++) {

				if( !vis[edges[j].u] ) {

					addedge(edges[j].u, con[Find(edges[j].u)]);

					vis[edges[j].u] = true;

				}

				if( !vis[edges[j].v] ) {

					addedge(edges[j].v, con[Find(edges[j].v)]);

					vis[edges[j].v] = true;

				}

			}

			lst = i;

		}

	}

	bfs(1);

	if( dis[N] == -1 ) puts("-1");

	else printf("%d\n", dis[N] / 2);

}

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

using namespace std;

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

#define f2(a,b,c) for(int c=a;c>=b;c--)

#define f3(a,b,c) for(int c=a;c;c=b)

#define so1(a,n) sort(a+1,a+n+1,mycmp);

#define so2(a,n) sort(a+1,a+n+1);

#define ll long long

#define itn int

#define ubt int 

#define pii pair<int,int>

#define mp make_pair

const int twx=1e6+100;

const int inf=0x7fffffff;

ll read()

{

    ll sum=0;

    ll flag=1;

    char c=getchar();

    while(c<'0'||c>'9')

    {

        if(c=='-')

        {

            flag=-1;

        }

        c=getchar();

    }

    while(c>='0'&&c<='9')

    {

        sum=((sum*10)+c-'0');

        c=getchar();

    }

    return sum*flag;

}

int n,m;

struct LV

{

    int Next;

    int y;

    int v;

}a[twx<<1];

int Link[twx];

int len;

map<pii,int> p;

int cnt=0;

bool v[twx];

int d[twx];

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

{

    a[++len].y=y;

    a[len].Next=Link[x];

    a[len].v=z;

    Link[x]=len;

}

int get(int x,int y)

{

    if(p.find(mp(x,y))!=p.end())

    {

        return p[mp(x,y)];     

    }

    else

    {

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

    }

}

void spfa()

{

    memset(d,0x3f,sizeof d);

    queue<int> q;

    q.push(1);

    v[1]=1;

    d[1]=0;

    while(!q.empty())

    {

        int x=q.front();

        q.pop();

        f3(Link[x],a[i].Next,i)

        {

            int y=a[i].y;

            int vs=a[i].v;

            if(d[y]>d[x]+vs)

            {

                d[y]=d[x]+vs;

                if(!v[y])

                {

                    v[y]=1;

                    q.push(y);

                }

            }

        }

        v[x]=0;

    }

}

void init()

{

	n=read();

    m=read();

    cnt=n;

    f1(1,m,i)

    {

        int x=read();

        int y=read();

        int z=read();

        int xz=get(x,z);

        int yz=get(y,z);

        Insert(xz,yz,0);

        Insert(yz,xz,0);

        Insert(x,xz,1);

        Insert(xz,x,1);

        Insert(y,yz,1);

        Insert(yz,y,1);

    }

    spfa();

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

}

void work()

{

	

}

void print()

{

	

}

int main()

{

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

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

    init();

	return 0;

}