/*  -*- coding: utf-8 -*-
 *
 * f.cc: F - Construct a Palindrome
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;

/* global variables */

vi nbrs[MAX_N][26];
int ds[MAX_N][MAX_N];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int u, v;
    char s[4];
    scanf("%d%d%s", &u, &v, s);
    u--, v--;
    int ci = s[0] - 'a';
    nbrs[u][ci].push_back(v);
    if (u != v) nbrs[v][ci].push_back(u);
  }

  memset(ds, -1, sizeof(ds));
  ds[0][n - 1] = 0;

  queue<pii> q;
  q.push(pii(0, n - 1));

  int mind = INF;
  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int u0 = u.first, u1 = u.second;
    int vd = ds[u0][u1] + 1;

    for (int c = 0; c < 26; c++) {
      vi &nbr0 = nbrs[u0][c], &nbr1 = nbrs[u1][c];
      if (! nbr0.empty() && ! nbr1.empty())
	for (vi::iterator vit0 = nbr0.begin(); vit0 != nbr0.end(); vit0++)
	  for (vi::iterator vit1 = nbr1.begin(); vit1 != nbr1.end(); vit1++) {
	    int v0 = *vit0, v1 = *vit1;
	    if (v0 == v1)
	      mind = min(mind, vd * 2);
	    else if (v0 == u1 && v1 == u0)
	      mind = min(mind, vd * 2 - 1);
	    else {
	      if (v0 > v1) swap(v0, v1);
	      if (ds[v0][v1] < 0) {
		ds[v0][v1] = vd;
		q.push(pii(v0, v1));
	      }
	    }
	  }
    }
  }
  
  printf("%d\n", (mind >= INF) ? -1 : mind);
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> T;
typedef pair<ll, vector<ll>> Pd;

const ll INF = 2e18;
const ll fact_table = 1200008;

priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
// priority_queue <ll, vector<ll>, greater<ll> > pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←

/*
#define endl "\n"
#ifdef ENJAPMA
#undef endl
#endif
*/

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)

typedef vector<ll> vec;
typedef vector<vector<ll>> mat;

// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言

const ll mod = 998244353ll;
// const ll mod = 1000000007ll;

ll mypow(ll a, ll b, ll m = mod) {
    ll x = 1;
    while (b) {
        while (!(b & 1)) {
            (a *= a) %= m;
            b >>= 1;
        }
        (x *= a) %= m;
        b--;
    }
    return x;
}
vec rv(ll read) {
    vec res(read);
    for (int i = 0; i < read; i++) {
        cin >> res[i];
    }
    return res;
}

/*
ll fact[fact_table + 5], rfact[fact_table + 5];

void c3_init() {
    fact[0] = rfact[0] = 1;
    for (ll i = 1; i <= fact_table; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
    for (ll i = fact_table; i >= 1; i--) {
        rfact[i - 1] = rfact[i] * i;
        rfact[i - 1] %= mod;
    }
    return;
}
ll c3(ll n, ll r) {
    return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/
bool icpc = false;
bool multicase = false;

ll n, m;
vector<P> G[500005];
ll cost[1005][1005];

bool solve() {
    cin >> n >> m;
    rep(i, m) {
        ll a, b;
        char c;
        cin >> a >> b >> c;
        G[a].pb(P(b, c - 'a'));
        G[b].pb(P(a, c - 'a'));
    }

    queue<P> q;
    rep2(i, 1, n) {
        rep2(j, 1, n) {
            cost[i][j] = INF;
        }
    }
    q.push(P(1, n));
    cost[1][n] = 0;

    while (q.size()) {
        auto [vx, vy] = q.front();
        q.pop();

        for (auto [nvx, cx] : G[vx]) {
            for (auto [nvy, cy] : G[vy]) {
                if (cx != cy) continue;
                if (cost[nvx][nvy] != INF) continue;

                q.push(P(nvx, nvy));
                cost[nvx][nvy] = cost[vx][vy] + 1;
            }
        }
    }

    ll ans = INF;

    for (int i = 1; i <= n; i++) {
        ans = min(ans, 2 * cost[i][i]);
    }

    for (int i = 1; i <= n; i++) {
        for (auto [nv, c] : G[i]) {
            int a = i, b = nv;
            ans = min(ans, cost[a][b] + cost[b][a]);
        }
    }

    if (ans == INF) ans = -1;
    p(ans);
    return true;
}

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

    if (icpc) {
        while (solve())
            ;
        return 0;
    }
    ll q, testcase = 1;
    if (multicase) {
        cin >> q;
    } else {
        q = 1;
    }
    while (q--) {
        solve();
        testcase++;
    }

    // solve();
    return 0;
}