#include <bits/stdc++.h>

#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> G[n][26];
    bool E[n][n];
    rep(i, n) rep(j, n) E[i][j] = false;
    rep(i, m) {
        int a, b;
        char c;
        scanf("%d%d %c", &a, &b, &c);
        --a;
        --b;
        G[a][c - 'a'].push_back(b);
        G[b][c - 'a'].push_back(a);
        E[a][b] = E[b][a] = true;
    }
    rep(i, n) rep(c, 26) {
        sort(G[i][c].begin(), G[i][c].end());
        G[i][c].erase(unique(G[i][c].begin(), G[i][c].end()), G[i][c].end());
    }

    int d[n][n];
    rep(i, n) rep(j, n) d[i][j] = mod;
    d[0][n - 1] = 0;
    queue<pii> que;
    que.emplace(0, n - 1);
    while (!que.empty()) {
        auto [u, v] = que.front();
        if (E[u][v]) {
            printf("%d\n", d[u][v] + 1);
            return 0;
        }
        que.pop();
        rep(c, 26) for (auto nu : G[u][c]) for (auto nv : G[v][c]) {
            if (chmin(d[nu][nv], d[u][v] + 2)) {
                if (nu == nv) {
                    printf("%d\n", d[nu][nv]);
                    return 0;
                }
                que.emplace(nu, nv);
            }
        }
    }
    puts("-1");
}
#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <cstdio>
#include <utility>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

using ll = long long int;

int n, m;
vector<int> g[1001][30];
int d[1001][1001];
bool g2[1001][1001];

int res;
constexpr int INF = 987654321;

int main(void)
{
#ifdef CUBE_PS
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    for(int i = 0; i < m; ++i) {
        int a, b;
        char c;
        cin >> a >> b >> c;
        g[a][c - 'a'].push_back(b);
        g[b][c - 'a'].push_back(a);
        g2[a][b] = true;
        g2[b][a] = true;
    }
    for(int i = 0; i <= n; ++i) {
        for(int j = 0; j <= n; ++j) {
            d[i][j] = INF;
        }
    }
    res = INF;

    queue<pair<int, int>> q;
    q.push({ 1, n });
    d[1][n] = 0;
    while(q.empty() == false) {
        int s = q.front().first;
        int e = q.front().second;
        q.pop();

        if(s == e) {
            res = min(res, d[s][e]);
        } else if(g2[s][e] == true) {
            res = min(res, d[s][e] + 1);
        }

        for(int i = 0; i <= 'z' - 'a'; ++i) {
            for(int ns : g[s][i]) {
                for(int ne : g[e][i]) {
                    if(d[ns][ne] > d[s][e] + 2) {
                        d[ns][ne] = d[s][e] + 2;
                        q.push({ ns, ne });
                    }
                }
            }

        }
    }

    if(res == INF) res = -1;
    cout << res;

    return 0;
}
