#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007

//ここから編集する
class UnionFind {
public:
    vector<int> p;//p[i]はiの属する組織
    UnionFind(int n) {
        p = vector<int>(n);
        for (int i = 0; i < n; i++) {
            p[i] = i;
        }
        return;
    }

    void printState() {
        if (DEBUG) {
            cout << "---" << endl;
            for (int i = 0; i < p.size(); i++) {
                printf("%dの親は%d\n", i, p[i]);
            }
            cout << "---" << endl;
        }
    }

/* xの属する集合を返す */
    int find(int x) {
        if (p[x] == x) return x;
        return p[x] = find(p[x]);
    }

/* yにxを統合する */
    void unite(int x, int y) {
        p[find(x)] = p[find(y)];
    }

/* xとyが属する集合が同じかを判定する */
    bool same(int x, int y) {
        return find(x) == find(y);
    }

};


class Dijkstra {
public:
    struct edge {
        ll to, cost;
    };
    typedef pair<ll, ll> P;//firstは最短距離、secondは頂点の番号

    int V;//超点数
    vector<vector<edge> > G;//グラフ,G[i]はiから出る辺の集合,G[i][j]はiから出る辺のj番目の辺
    vector<ll> d; //最短距離

    //0origin
    Dijkstra(int N) {
        V = N;
        G = vector<vector<edge> >(N);
        d = vector<ll>(N);
    };

    void dijkstra(int s) {
        priority_queue<P, vector<P>, greater<P> > que;
        fill(d.begin(), d.end(), INFl);
        d[s] = 0;
        que.push(P(0, s));

        while (!que.empty()) {
            P p = que.top();
            que.pop();
            ll v = p.second;
            if (d[v] < p.first) continue;
            for (int i = 0; i < G[v].size(); i++) {
                edge e = G[v][i];
                if (d[e.to] > d[v] + e.cost && !(d[e.to] == INFl && d[v] == INFl)) {
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }

    }

    /* 頂点fromに頂点toに達するコストcostの辺を貼る */
    void addEdge(int from, int to, int cost) {
        edge e;
        e.to = to;
        e.cost = cost;
        G[from].push_back(e);
    }
};

template<typename T>
vector<int> zaatsu(vector<T> v) {
    map<T, int> mp;
    vector<T> w = v;
    sort(all(w));
    int cnt = 0;
    for (int i = 0; i < w.size(); i++) {
        if (mp.find(w[i]) == mp.end()) {
            mp[w[i]] = ++cnt;
        }
    }
    vector<int> ret(v.size());

    for (int i = 0; i < v.size(); i++) {
        ret[i] = mp[v[i]];
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    map<int, vector<pair<int, int> > > cpq;
    rep(i, 0, M) {
        int p, q, c;
        cin >> p >> q >> c;
        cpq[c].emplace_back(p, q);
    }

    UnionFind uf(N + 1),uf2(11);
    uf2 = uf;

    Dijkstra dij(N + M + 5);
    int cnt = N + 1;
    for (auto it : cpq) {
//        uf = uf2;
        set<int> used;
        vector<pair<int, int> > es = it.second;
        for (auto jt : es) {
            int p = jt.first;
            int q = jt.second;
            used.insert(p);
            used.insert(q);

            uf.unite(p, q);
        }
        map<int, int> eg;
        map<int, int> shu;
        int cntshu = 0;
        for (auto jt : used) {
            eg[jt] = uf.find(jt);
            if (shu.find(uf.find(jt)) == shu.end()) {
                shu[uf.find(jt)] = ++cntshu;
            }
            dij.addEdge(jt, shu[eg[jt]] + cnt, 1);
            dij.addEdge(shu[eg[jt]] + cnt, jt, 1);

        }
        for(auto jt : used){
            uf.p[jt] = jt;
        }

        cnt += shu.size();
    }
    dij.dijkstra(1);
    ll ans = dij.d[N] / 2;
    if (ans == INFl / 2) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 51832