#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct State {
  int begin, end, cost;
  State(int begin_, int end_, int cost_) : begin(begin_), end(end_), cost(cost_) { }
};
bool done[1000][1000];
int main() {
  int N, M; cin >> N >> M;
  vector<vector<int>> G(N);
  vector<vector<char>> C(N);
  for(int i = 0; i < M; i++) {
    int a, b; char c;
    cin >> a >> b >> c;
    a--; b--;
    G[a].push_back(b);
    C[a].push_back(c);
    G[b].push_back(a);
    C[b].push_back(c);
  }
  queue<State> Q;
  Q.emplace(0, N - 1, 0);
  done[0][N - 1] = true;
  int ans = 1e9;
  while(!Q.empty()) {
    State s = Q.front();
    Q.pop();
    for (int i = 0; i < (int)G[s.begin].size(); i++) {
      for (int j = 0; j < (int)G[s.end].size(); j++) {
        if(C[s.begin][i] != C[s.end][j]) {
          continue;
        }
        int u = G[s.begin][i];
        int v = G[s.end][j];
        if(u == s.end && v == s.begin) {
          ans = min(ans, s.cost + 1);
        }
        if(u == v) {
          ans = min(ans, s.cost + 2);
        }
        if(done[u][v]) {
          continue;
        }
        done[u][v] = true;
        Q.emplace(u, v, s.cost + 2);
        // cerr << "(" << u+1 << "," << v+1 << ") = " << s.cost + 2 << endl;
      }
    }
  }
  if (ans == 1e9) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef tuple<int,int,int> tpl;
typedef pair<int,tpl> Pair;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

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

struct Edge{
    int from, to, id=-1;
    int cost=1;

    Edge(int from, int to, int cost=1, int id=-1): from(from), to(to), cost(cost), id(id) {}
    Edge() {}

    friend istream &operator>>(istream  &input, Edge &e ){ 
        char c;
        input >> e.from >> e.to >> c;
        e.from--; e.to--; e.cost = c-'a';
        return input;            
    }

    Edge rev(void){
        return Edge(to, from, cost, id);
    }
};

vector<vector<Edge>> edge(1010);
bool connect[1010][1010];
int d[1010][1010][27];

void Main(){
    int N,M; cin >> N >> M;
    vector<Edge> edge2[N][26];
    REP(_,M){
        Edge e; cin >> e;
        edge[e.from].emplace_back(e);
        edge[e.to].emplace_back(e.rev());

        connect[e.from][e.to] = connect[e.to][e.from] = true;

        edge2[e.from][e.cost].emplace_back(e);
        edge2[e.to][e.cost].emplace_back(e.rev());
    }
    REP(i,N)REP(j,N){
        REP(x,27) d[i][j][x] = INF;
    }

    if(connect[0][N-1]){
        cout << 1 << en;
        return;
    }

    int ans = INF;
    priority_queue<Pair,vector<Pair>,greater<Pair>> q;
    q.emplace(0,tpl(0,N-1,26));
    while(!q.empty()){
        auto p = q.top(); q.pop();
        int cost = p.first, x, y, c;
        tie(x,y,c) = p.second;

        if(!chmin(d[x][y][c], cost)) continue;
        if(x==y){
            chmin(ans, cost);
            continue;
        }
        if(connect[x][y]){
            chmin(ans, cost+1);
            continue;
        }

        REP(i,26){
            for(auto& ex : edge2[x][i])for(auto& ey : edge2[y][i]){
                q.emplace(cost+2, tpl(ex.to,ey.to,i));
            }
        }
    }

    cout << (ans<INF ? ans : -1) << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    while(t--) Main();
    return 0;
}