// Slower C++ solution:
#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]);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.385232631620242x):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>

#define MN 301000

#define MM 1601000



typedef std::pair<int, int> P;

int h[MN], nxt[MM], to[MM], cost[MM], K = 0;

std::vector<P> v[101000];

int d[MN], n, m;

std::deque<int> q;



void ins(int u, int v, int c) {nxt[++K] = h[u]; h[u] = K; to[K] = v; cost[K] = c;}



int bfs()

{

	q.push_back(1);

	memset(d, 0x3f, sizeof(d)); d[1] = 0;

	while(!q.empty())

	{

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

		for(int i = h[u]; i; i = nxt[i])

		{

			if(d[to[i]] > d[u] + cost[i])

			{

				d[to[i]] = d[u] + cost[i];

				if(cost[i]) q.push_back(to[i]);

				else q.push_front(to[i]);

			}

		}

	}

	if(d[n] > 1e9) return -1;

	return d[n];

}



int main()

{

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

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

	{

		int p, q, c; scanf("%d%d%d", &p, &q, &c);

		v[p].push_back(P(c, i));

		v[q].push_back(P(c, i));

		ins(p, n + i, 1); ins(n + i, p, 0);

		ins(q, n + i, 1); ins(n + i, q, 0);

	}

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

	{

		std::sort(v[i].begin(), v[i].end());

		int S = v[i].size();

		for(int j = 1; j < S; j++)

		{

			if(v[i][j].first == v[i][j - 1].first)

			{

				ins(v[i][j].second + n, v[i][j - 1].second + n, 0);

				ins(v[i][j - 1].second + n, v[i][j].second + n, 0); 

			}

		}

	}

	printf("%d\n", bfs());

}