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

        }

        //system("Pause");

        return 0;

}


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

#define LL long long

#define MEM(x,y) memset(x,y,sizeof(x))

#define MOD(x) ((x)%mod)

#define mod 1000000007

#define pb push_back

#define pii pair<int, int>

#define mkpii make_pair

#define STREAM_FAST ios::sync_with_stdio(false)

using namespace std;

const int maxn = 1e6 + 7;

struct node

{

	int v, w, next;

	node(int v, int w) : v(v), w(w) {}

	node(){}

	bool operator < (const node &rhs) const 

	{

		return w > rhs.w;

	}

}e[maxn << 1];

map<pii, int> mp;

int n, m;

int head[maxn], tot = 0;

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

{

	++tot; 

	e[tot].v = v; e[tot].w = w; 

	e[tot].next = head[u]; head[u] = tot;

}

int getid(int x, int y)

{

	pii tmp = mkpii(x, y);

	if (!mp.count(tmp)) mp[tmp] = ++n;

	return mp[tmp];

}

priority_queue<node>q;

int dis[maxn], vis[maxn];

int dijkstra(int s, int t)

{

	MEM(dis, 0x3f3f3f3f);

	q.push(node(s, 0)); dis[s] = 0;

	while (!q.empty())

	{

		int u = q.top().v; q.pop();

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

		for (int i = head[u]; i; i = e[i].next)

		{

			int v = e[i].v;

			if (dis[v] > dis[u] + e[i].w)

			{

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

				q.push(node(v, dis[v]));

			}

		}

	}

	return dis[t] >= 0x3f3f3f3f ? -1 : dis[t] / 2;

}

int main()

{

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

	int s = 1, t = n;

	while (m--)

	{

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

		int uc = getid(u, c), vc = getid(v, c);

		add(u , uc, 1); add(uc, u, 1);

		add(uc, vc, 0); add(vc, uc, 0);

		add(vc, v, 1); add(v, vc, 1);

	}

	printf("%d\n", dijkstra(s, t));

	return 0;

}