#include<bits/stdc++.h>
#define int long long
#define PI pair<int,int>
using namespace std;
const int maxm=1e3+5;
//const int mod=1e9+7;
//const int mod=998244353;
vector<PI>g2[maxm][maxm];
vector<PI>g[maxm];
int mark[maxm][maxm];
int d[maxm][maxm];
int n,m;
void bfs(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            d[i][j]=1e18;
        }
    }
    queue<PI>q;
    q.push({1,n});
    d[1][n]=0;
    mark[1][n]=1;
    while(q.size()){
        PI x=q.front();q.pop();
        for(auto y:g2[x.first][x.second]){
            if(!mark[y.first][y.second]){
                mark[y.first][y.second]=1;
                d[y.first][y.second]=d[x.first][x.second]+1;
                q.push(y);
            }
        }
    }
}
void solve(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        char c;cin>>c;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            for(auto x:g[i]){
                for(auto y:g[j]){
                    if(x.second==y.second){
                        g2[i][j].push_back({x.first,y.first});
                    }
                }
            }
        }
    }
    bfs();
    int ans=1e18;
    //偶数长度路径
    for(int i=1;i<=n;i++){
        ans=min(ans,d[i][i]*2);
    }
    //奇数长度路径,枚举中间的边
    for(int i=1;i<=n;i++){
        for(auto p:g[i]){
            int j=p.first;
            ans=min(ans,d[i][j]*2+1);
        }
    }
    //
    if(ans==1e18)ans=-1;//无解
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);
    solve();
    return 0;
}
/*


*/

#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> tpl;
typedef pair<int,int> 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];

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) d[i][j] = INF;
    int ans = INF;
    queue<Pair> q;
    q.emplace(0,N-1);
    while(!q.empty()){
        auto p = q.front(); q.pop();
        int cost = p.first, v = p.second;
        int x = v/N, y = v%N;

        if(cost >= ans) break;

        if(!chmin(d[x][y], cost)) continue;
        if(x==y){
            chmin(ans, cost);
            break;
        }
        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, ex.to*N+ey.to);
            }
        }
    }

    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;
}