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

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

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;

        while (scanf("%d %d", &n, &m) == 2)

        {

                //cout << n << " " << m << endl;

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

                {

                        h[i] = 0;

                        ans[i] = inf;

                        comp[i].clear();

                }

                tot = 0;

                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]);

        }

        return 0;

}

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

#include <string.h>

#include <map>

#include <queue>

#define MN 100000

#define MM 200000

int n,m,vn;

int hd[MN*5+5],to[MM*6+5],nxt[MM*6+5],val[MM*6+5];

int dis[MN*5+5];

bool vis[MN*5+5];

void add(int u,int v,int w){

	static int rn=0;

	to[rn]=v,nxt[rn]=hd[u],val[rn]=w;

	hd[u]=rn++;

}

int gid(int u,int c){

	static std::map<int,int> tb[MN+5];

	if(tb[u].find(c)==tb[u].end()){

		tb[u][c]=(++vn);

		add(u,tb[u][c],1);

		add(tb[u][c],u,0);

	}

	return tb[u][c];

}

void bfs01(){

	static std::deque<int> q;

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

	dis[1]=0; q.push_front(1);

	while(!q.empty()){

		int u=q.front(); q.pop_front();

		if(vis[u]) continue; vis[u]=true;

		for(int i=hd[u];~i;i=nxt[i]){

			if(vis[to[i]]) continue;

			if(dis[u]+val[i]<dis[to[i]]){

				dis[to[i]]=dis[u]+val[i];

				if(val[i]==0)

					q.push_front(to[i]);

				else

					q.push_back(to[i]);

			}

		}

	}

}

int main(){

	memset(hd,0xff,sizeof(hd));

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

	vn=n;

	for(int i=1,u,v,c;i<=m;i++){

		scanf("%d%d%d",&u,&v,&c);

		u=gid(u,c),v=gid(v,c);

		add(u,v,0),add(v,u,0);

	}

	bfs01();

	if(dis[n]==0x3f3f3f3f) puts("-1");

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

}